DEV Community

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

Posted on • Edited on

String in Python (5)

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-Type:int or NoneType): *Memos:
    • If it's not set or None, 0 is set.
    • Don't use start.
  • The 3rd argument is end(Optional-Type:int or NoneType): *Memos:
    • If it's not set or None, 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')) print(v.count('l', None, None)) # 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 
Enter fullscreen mode Exit fullscreen mode
v = '' print(v.count('')) # 1 
Enter fullscreen mode Exit fullscreen mode

startswith() can check if it's the prefix of a string as shown below:

*Memos:

  • The 1st argument is prefix(Required-Type:str): *Memos:
    • If it's an empty string, True is returned.
    • Don't use prefix=.
  • The 2nd argument is start(Optional-Type:int or NoneType): *Memos:
    • If it's not set or None, 0 is set.
    • Don't use start.
  • The 3rd argument is end(Optional-Type:int or NoneType): *Memos:
    • If it's not set or None, the length + 1 is set.
    • Don't use end.
v = 'hello world' print(v.startswith('he')) # True print(v.startswith('he', None, None)) # True print(v.startswith('HE')) # False print(v.startswith('wo')) # False  print(v.startswith(('he', 'wo'))) # True print(v.startswith(('wo', 'he'))) # True print(v.startswith(('wo', 'lo'))) # False  print(v.startswith('he', 0)) # True print(v.startswith('he', 1)) # False  print(v.startswith('he', 0, 1)) # False print(v.startswith('he', 0, 2)) # True  print(v.startswith('wo', 6, 7)) # False print(v.startswith('wo', 6, 8)) # True  print(v.startswith('')) # True 
Enter fullscreen mode Exit fullscreen mode
v = '' print(v.startswith('')) # True 
Enter fullscreen mode Exit fullscreen mode

endswith() can check if it's the suffix of a string as shown below:

*Memos:

  • The 1st argument is suffix(Required-Type:str): *Memos:
    • If it's an empty string, True is returned.
    • Don't use suffix=.
  • The 2nd argument is start(Optional-Type:int or NoneType): *Memos:
    • If it's not set or None, 0 is set.
    • Don't use start.
  • The 3rd argument is end(Optional-Type:int or NoneType): *Memos:
    • If it's not set or None, the length + 1 is set.
    • Don't use end.
v = 'hello world' print(v.endswith('ld')) # True print(v.endswith('ld', None, None)) # True print(v.endswith('LD')) # False print(v.endswith('lo')) # False  print(v.endswith(('ld', 'lo'))) # True print(v.endswith(('lo', 'ld'))) # True print(v.endswith(('lo', 'wo'))) # False  print(v.endswith('ld', 9)) # True print(v.endswith('ld', 10)) # False  print(v.endswith('ld', 9, 10)) # False print(v.endswith('ld', 9, 11)) # True  print(v.endswith('lo', 3, 4)) # False print(v.endswith('lo', 3, 5)) # True  print(v.endswith('')) # True 
Enter fullscreen mode Exit fullscreen mode
v = '' print(v.endswith('')) # True 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)