String in Python (5)



This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)

Buy Me a Coffee☕

*Memos:

count() can count a set of zero or more characters of a string as shown below:

*Memos:

  • The 1st argument is sub(Required-Type:str). *Don’t use sub=.
  • The 2nd argument is start(Optional-Default:None-Type:int or NoneType): *Memos:
    • If it’s not set, 0 is set.
    • Don’t use start.
  • The 3rd argument is end(Optional-Default:None-Type:int or NoneType): *Memos:
    • If it’s not set, the length + 1 is set.
    • Don’t use end.
  • If sub is an empty string and if start and end aren’t set, the length + 1 is returned.
v = 'hello world'

print(v.count('l')) # 3
print(v.count('o')) # 2
print(v.count('d')) # 1

print(v.count('l', 3))
print(v.count('l', 3, 11))
# 2

print(v.count('l', 3, 9)) # 1

print(v.count('L'))
print(v.count('a'))
# 0

print(v.count('ll'))
print(v.count('lo'))
print(v.count('wo'))
# 1

print(v.count(''))
print(v.count('', 0, 11))
# 12

print(v.count('', 0, 4))
print(v.count('', 3, 7))
print(v.count('', 7, 11))
# 5

expandtabs() can replace \t with zero or more spaces as shown below. *The 1st argument is tabsize(Optional-Default:8-Type:int).

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
#   ↑↑↑↑↑    ↑↑↑
v = "12\t1234\t1\t123\n1234\t1\t123\t12"

print(v.expandtabs())
print(v.expandtabs(tabsize=8))
# 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

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). *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
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


This content originally appeared on DEV Community and was authored by Super Kai (Kazuya Ito)