Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
executable file
·
52 lines (32 loc) · 1.12 KB

File metadata and controls

executable file
·
52 lines (32 loc) · 1.12 KB
Copy raw file
Download raw file
Outline
Edit and raw actions

Formatting strings and output

Examples in Python 3

"""
Format strings for fancier output
"""
# Add variable values to a string
x = "{0} ants are more than {1} elephants".format("Five", 4)
print(x)

x = "Five"
y = 4
print("{0} ants are more than {1} elephants".format(x, y))

# With named parameters
print("My name is {name} and I am {age} years old.".format(age=18, name="Mumintrollet"))

# Add thousands separator
print("{:,}".format(1000000))

# Fixed floating point
print("{:.2f}".format(1.2345))

print("{:.3f}".format(42))

# Fixed character width
print("{0:10}{1:10}".format("Hello", "World"))

Reference and read more

String Formatting

Format String Syntax

str.format()

Fancier Output Formatting

Revision history

2014-07-16 (sylvanas) PA1 First try.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.