String in Python (5)

Buy Me a Coffee☕

*Memos:

My post explains a string.

My post explains str().

My post explains encode() and decode().

My post explains upper(), lower(), casefold(), swapcase(), title(), capitalize(), isupper(), islower() and istitle().

count() c…


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)


Print Share Comment Cite Upload Translate Updates
APA

Super Kai (Kazuya Ito) | Sciencx (2025-06-29T00:24:15+00:00) String in Python (5). Retrieved from https://www.scien.cx/2025/06/29/string-in-python-5/

MLA
" » String in Python (5)." Super Kai (Kazuya Ito) | Sciencx - Sunday June 29, 2025, https://www.scien.cx/2025/06/29/string-in-python-5/
HARVARD
Super Kai (Kazuya Ito) | Sciencx Sunday June 29, 2025 » String in Python (5)., viewed ,<https://www.scien.cx/2025/06/29/string-in-python-5/>
VANCOUVER
Super Kai (Kazuya Ito) | Sciencx - » String in Python (5). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/06/29/string-in-python-5/
CHICAGO
" » String in Python (5)." Super Kai (Kazuya Ito) | Sciencx - Accessed . https://www.scien.cx/2025/06/29/string-in-python-5/
IEEE
" » String in Python (5)." Super Kai (Kazuya Ito) | Sciencx [Online]. Available: https://www.scien.cx/2025/06/29/string-in-python-5/. [Accessed: ]
rf:citation
» String in Python (5) | Super Kai (Kazuya Ito) | Sciencx | https://www.scien.cx/2025/06/29/string-in-python-5/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.