DEV Community

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

Posted on • Edited on

String in Python (12)

Buy Me a Coffee

*Memos:

strip() can remove zero or more characters(chars) from the left and right character of a string one by one as shown below:

*Memos:

  • The 1st argument is chars(Optional-Defualt:None-Type:str or NoneType): *Memos:
    • It's the zero or more characters to remove from the left and right character of a string one by one.
    • Its each character is considered one by one so it's not a prefix and suffix.
    • If it's not set or None, " " is set.
    • Don't use chars=.
v = " aa bb cc " # ↑↑ ↑↑ print('"' + v.strip() + '"') print('"' + v.strip(" ") + '"') print('"' + v.strip(" ABC ") + '"') # "aa bb cc"  print('"' + v.strip("a ") + '"') print('"' + v.strip(" a") + '"') # "bb cc"  print('"' + v.strip("c ") + '"') print('"' + v.strip(" c") + '"') # "aa bb"  print('"' + v.strip("ac ") + '"') print('"' + v.strip(" ac") + '"') print('"' + v.strip("a c") + '"') # "bb"  print('"' + v.strip("") + '"') print('"' + v.strip("a") + '"') print('"' + v.strip("c") + '"') print('"' + v.strip("ac") + '"') # " aa bb cc " # ↑↑ ↑↑ 
Enter fullscreen mode Exit fullscreen mode

lstrip() can remove zero or more characters(chars) from the left character of a string one by one as shown below:

*Memos:

  • The 1st argument is chars(Optional-Defualt:None-Type:str or NoneType): *Memos:
    • It's the zero or more characters to remove from the left character of a string one by one.
    • Its each character is considered one by one so it's not a prefix.
    • If it's not set or None, " " is set.
    • Don't use chars=.
v = " aa bb cc " print('"' + v.lstrip() + '"') print('"' + v.lstrip(" ") + '"') print('"' + v.lstrip("c ") + '"') print('"' + v.lstrip(" c") + '"') print('"' + v.lstrip(" ABC ") + '"') # "aa bb cc "  print('"' + v.lstrip("a ") + '"') print('"' + v.lstrip(" a") + '"') print('"' + v.lstrip("ac ") + '"') print('"' + v.lstrip(" ac") + '"') print('"' + v.lstrip("a c") + '"') # "bb cc "  print('"' + v.lstrip("") + '"') print('"' + v.lstrip("a") + '"') print('"' + v.lstrip("c") + '"') print('"' + v.lstrip("ac") + '"') # " aa bb cc " 
Enter fullscreen mode Exit fullscreen mode

rstrip() can remove zero or more characters(chars) from the right character of a string one by one as shown below:

*Memos:

  • The 1st argument is chars(Optional-Defualt:None-Type:str or NoneType): *Memos:
    • It's the zero or more characters to remove from the right character of a string one by one.
    • Its each character is considered one by one so it's not a suffix.
    • If it's not set or None, " " is set.
    • Don't use chars=.
v = " aa bb cc " print('"' + v.rstrip() + '"') print('"' + v.rstrip(" ") + '"') print('"' + v.rstrip("a ") + '"') print('"' + v.rstrip(" a") + '"') print('"' + v.rstrip(" ABC ") + '"') # " aa bb cc"  print('"' + v.rstrip("c ") + '"') print('"' + v.rstrip(" c") + '"') print('"' + v.rstrip("ac ") + '"') print('"' + v.rstrip(" ac") + '"') print('"' + v.rstrip("a c") + '"') # " aa bb"  print('"' + v.rstrip("") + '"') print('"' + v.rstrip("a") + '"') print('"' + v.rstrip("c") + '"') print('"' + v.rstrip("ac") + '"') # " aa bb cc " 
Enter fullscreen mode Exit fullscreen mode

isspace() can check if a string only has ASCII whitespaces and isn't empty as shown below. *It has no arguments:

print(' '.isspace()) print(' '.isspace()) print('\t\n\r\v\x0b\f\x0c\x1c\x1d\x1e\x85\u2028\u2029'.isspace()) # True  print('Python 3'.isspace()) print(''.isspace()) # False 
Enter fullscreen mode Exit fullscreen mode

Top comments (0)