python string format function
we can use the formatted string with format() function in python.
the declaration is like below
in order to check it, I typed codes like below.
you can learn something from above lines.
it's time to check the result of above codes.
is it same to your guess?
the declaration is like below
str.format(*args, **kwargs)let's check how to use the function in the example.
in order to check it, I typed codes like below.
1
2
3
4
5
6
7
|
print("my favorite fruit is {}".format("apple"))
print("{}'s favorite fruit is {}".format("apple", "he"))
print("{1}'s favorite fruit is {0}".format("apple", "he"))
print("{human}'s favorite fruit is {fruit}".format(**{"human": "she", "fruit": "banana"}))
| cs |
you can learn something from above lines.
- we can set the format filed with braces {}.
- each argument has position and the position is used as index in braces {}
- dictionary can be argument and the key is used in each format filed.
it's time to check the result of above codes.
is it same to your guess?
Comments
Post a Comment