python string format function

we can use the formatted string with format() function in python.

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.


  1. we can set the format filed with braces {}.
  2. each argument has position and the position is used as index in braces {}
  3. 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

Popular posts from this blog

making menubar and menu on QML

QSizePolicy Fixed example in pyside2

let's start QML programming with PySide2