Monday, January 6, 2025
HomeProgrammingPython String Format () Method

Python String Format () Method

 

The Python format() method is a versatile string formatting tool that allows embedding dynamic values within a string. It uses curly braces {} as placeholders, which are replaced by values provided as arguments. It supports positional and keyword arguments, number formatting, alignment, and padding.

See also  HTML Tag

Example:

name = “Alice”
age = 30
print(“Name: {}, Age: {}”.format(name, age))
# Output: Name: Alice, Age: 30

Features:

Positional and Keyword Arguments: {0}, {name}

Number Formatting: {:.2f} for decimals.

Alignment: {:>10} (right), {:^10} (center), {:*^10} (padded).

The format() method is compatible with Python 2.6+ and Python 3, offering a readable alternative to older formatting techniques

RELATED ARTICLES
0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
- Advertisment -

Most Popular

Recent Comments

0
Would love your thoughts, please comment.x
()
x