Python String format() Method
The format() function formats complex strings more efficiently. By placing one or more surrogate fields inside a pair of curly braces {} in a string and calling the value we wish to put and concatenating it to the string.
How to formulate it like this:
using a single value.
{ }.format(value)
using multiple values.
{ } { } .format(value1, value2)
value : can be an integer, a float, a string, a character, or even a variable.
return value: Returns a new string formatted with the value passed as a parameter at the position of the field alt{} .
Errors and exceptions: Occurs when the string contains an additional surrogate field {} , and we have not passed any value to it.
Example:
# using format option in a simple string
print(" {} learn Python " .format("Hamza"))
# value stored in a variable
string = "You can learn {} and {} with Ahmed Obaid"
print(string.format("python", "django" ))
# formatting a string using a numeric constant
print("I am {} years old !".format(38))
The output will be:
Hamza learn Python
You can learn python and django with Ahmed Obaid
I am 38 years old !
Note: Alternate fields are the places to pass values that are enclosed in curly braces {} .
In the following example we will use multiple values for several alternate fields.
Example:
string = "I am {}, I will present on this site courses in {}, {}, and {}"
print(string.format("ahmed obaid","python", "django", "Data science"))
The output will be:
I am ahmed obaid, I will present on this site courses in python, django, and Data science
If the number of values is more or less than the number of alternate fields, we will get an IndexError.
Example:
string = "I am {}, I will present on this site courses in {}, {}, and {}"
print(string.format("python", "django", "Data science"))
We will get this error:
Traceback (most recent call last):
File "./prog.py", line 3, in <module>
IndexError: Replacement index 3 out of range for positional args tuple
In the previous example, the number of values was three: "python", "django", "Data science". And the number of alternative fields available four fields undefined So an error occurred.
Also, the surrogate fields must be set with the index of the values in order such as 0, 1, 2, 3… Observe the following example
Example:
string = "I am {} {} "
print(string.format("Ahmed", "obaid"))
string = "I am {} {} "
print(string.format("obaid","Ahmed"))
The output will be:
I am Ahmed obaid
I am obaid Ahmed
You can also pass the index numbers of the values inside the brackets of the alternate fields. Where the starting index of the values will be the number 0.
Example:
# We have correctly ordered the index of values with surrogate fields
string = "I am {0} {1} "
print(string.format("Ahmed", "obaid"))
# Here we changed the index order of the values with the surrogate fields
string = "I am {1} {0} "
print(string.format("Ahmed", "obaid"))
# We have correctly ordered the index of values with surrogate fields
string = "I am {0}, I will present on this site courses in {1}, {2}, and {3}"
print(string.format("Ahmed obaid", "python", "django", "Data science"))
# Here we changed the index order of the values with the surrogate fields
string = "I am {0}, I will present on this site courses in {3}, {1}, and {2}"
print(string.format("Ahmed obaid", "python", "django", "Data science"))
The output will be:
I am Ahmed obaid
I am obaid Ahmed
I am Ahmed obaid, I will present on this site courses in python, django, and Data science
I am Ahmed obaid, I will present on this site courses in Data science, python, and django
External sources:
Built-in functions - official Python documentation
If you have any questions or concerns, leave them in the comments
Tags
Python python data types Python String string methods python python string format method
Share page
About author
Ahmed Obaid
Hello, I am Ahmed Obaid, an Egyptian Arabic programmer. I would like to put my experiences learning Python on this site So that it will be a reference for you and me as well.
Sign up to leave a comment