Git Product home page Git Product logo

starting_python's Introduction

Python Notes

printing with various format placeholders

In Python f-strings, placeholders are enclosed in curly braces {} and can contain various format specifiers and expressions to format values. Here are some common placeholders and format specifiers you can use within f-strings:

  1. Basic Value Insertion:

    • {variable}: Insert the value of the variable.
    • {expression}: Insert the result of the expression.
  2. String Formatting:

    • {variable:s}: Insert the value of the variable as a string.
    • {variable:<width}: Left-align the string within a field of the specified width.
    • {variable:^width}: Center-align the string within a field of the specified width.
    • {variable:>width}: Right-align the string within a field of the specified width.
    • {variable:.precisionf}: Format a floating-point number with the specified precision.
    • {variable:.precisions}: Format a string with the specified length (right-padded with spaces if needed).
  3. Integer Formatting:

    • {variable:d}: Insert the value of the variable as an integer.
    • {variable:widthd}: Format an integer with a minimum field width.
    • {variable:0widthd}: Format an integer with leading zeros.
  4. Floating-Point Formatting:

    • {variable:f}: Insert the value of the variable as a floating-point number.
    • {variable:.precisionf}: Format a floating-point number with the specified precision.
    • {variable:e}: Format a floating-point number in scientific notation.
    • {variable:.precisione}: Format a floating-point number in scientific notation with the specified precision.
  5. Hexadecimal and Binary Formatting:

    • {variable:x}: Format an integer as a hexadecimal string (lowercase).
    • {variable:X}: Format an integer as a hexadecimal string (uppercase).
    • {variable:b}: Format an integer as a binary string.
  6. Date and Time Formatting:

    • {variable:%format}: Format a datetime object using Python's strftime format.

For example, here's how you can use some of these placeholders and format specifiers in f-strings:

name = "Alice"
age = 30
pi = 3.14159

print(f"Hello, {name}! You are {age} years old.")
print(f"Value of pi: {pi:.2f}")
print(f"Binary representation of 10: {10:b}")

starting_python's People

Contributors

loukhai avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.