DEV Community

Super Kai (Kazuya Ito)
Super Kai (Kazuya Ito)

Posted on • Edited on

String in Python (11)

Buy Me a Coffee

*Memos:

zfill() can add the one or more 0s before the string set width as shown below:

*Memos

  • The 1st argument is width(Required-Type:int): *Memos:
    • It decides the width of a string.
    • Don't use width=.
  • If the 1st character of a string is + or -, the one or more 0s are added after it.
s = '1234' print(s.zfill(4)) # 1234 print(s.zfill(5)) # 01234 print(s.zfill(6)) # 001234 print(s.zfill(7)) # 0001234 print(s.zfill(8)) # 00001234  print('+1234'.zfill(8)) # +0001234 print('-1234'.zfill(8)) # -0001234 print('+-1234'.zfill(8)) # +00-1234 print('-+1234'.zfill(8)) # -00+1234 print('!1234'.zfill(8)) # 000!1234 
Enter fullscreen mode Exit fullscreen mode
s = 'abcd' print(s.zfill(4)) # abcd print(s.zfill(5)) # 0abcd print(s.zfill(6)) # 00abcd print(s.zfill(7)) # 000abcd print(s.zfill(8)) # 0000abcd  print('+abcd'.zfill(8)) # +000abcd print('-abcd'.zfill(8)) # -000abcd print('+-abcd'.zfill(8)) # +00-abcd print('-+abcd'.zfill(8)) # -00+abcd print('!abcd'.zfill(8)) # 000!abcd 
Enter fullscreen mode Exit fullscreen mode

expandtabs() can replace \t with zero or more spaces as shown below:

*Memos:

  • The 1st argument is tabsize(Optional-Default:8-Type:int): *Memos:
    • It decides tab size to replace \t with zero or more spaces.
    • The number of spaces depending on the word before \t.
v = 'We\tlike\tapples' print(v.expandtabs()) print(v.expandtabs(tabsize=8)) # We like apples # ↑↑↑↑↑↑ ↑↑↑↑  print(v.expandtabs(tabsize=0)) # Welikeapples  print(v.expandtabs(tabsize=1)) # We like apples # ↑ ↑  print(v.expandtabs(tabsize=2)) # We like apples # ↑↑ ↑↑  print(v.expandtabs(tabsize=3)) # We like apples # ↑ ↑↑  print(v.expandtabs(tabsize=4)) # We like apples # ↑↑ ↑↑↑↑  print(v.expandtabs(tabsize=5)) # We like apples # ↑↑↑ ↑  print(v.expandtabs(tabsize=6)) # We like apples # ↑↑↑↑ ↑↑  print(v.expandtabs(tabsize=7)) # We like apples # ↑↑↑↑↑ ↑↑↑ 
Enter fullscreen mode Exit fullscreen mode
v = "12\t1234\t1\t123\n1234\t1\t123\t12" print(v.expandtabs()) # 12 1234 1 123 # 1234 1 123 12  print(v.expandtabs(tabsize=0)) # 1212341123 # 1234112312  print(v.expandtabs(tabsize=1)) # 12 1234 1 123 # 1234 1 123 12  print(v.expandtabs(tabsize=2)) # 12 1234 1 123 # 1234 1 123 12  print(v.expandtabs(tabsize=3)) # 12 1234 1 123 # 1234 1 123 12  print(v.expandtabs(tabsize=4)) # 12 1234 1 123 # 1234 1 123 12  print(v.expandtabs(tabsize=5)) # 12 1234 1 123 # 1234 1 123 12  print(v.expandtabs(tabsize=6)) # 12 1234 1 123 # 1234 1 123 12  print(v.expandtabs(tabsize=7)) # 12 1234 1 123 # 1234 1 123 12 
Enter fullscreen mode Exit fullscreen mode

format_map() can format and create a string with a dictionary as shown below:

*Memos:

  • The 1st argument is mapping(Required-Type:dict). *Don't use mapping=.
  • {} can do value replacement.
v1 = {'name':'John','age':36} v2 = "I'm {name} and {age} years old." print(v2.format_map(v1)) # I'm John and 36 years old. 
Enter fullscreen mode Exit fullscreen mode
v1 = {'name':{'fname':'John', 'lname':'Smith'}, 'age':36} v2 = "I'm {name[fname]} {name[lname]} and {age} years old." print(v2.format_map(v1)) # I'm John Smith and 36 years old. 
Enter fullscreen mode Exit fullscreen mode
v1 = {'name':['John', 'Smith'],'age':36} v2 = "I'm {name[0]} {name[1]} and {age} years old." print(v2.format_map(v1)) # I'm John Smith and 36 years old. 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)