python

Python rstrip函数和strip函数的区别

小樊
113
2024-07-25 03:46:09
栏目: 编程语言

Python中的rstrip()函数和strip()函数都是用来去除字符串中指定的字符或空格的方法,但它们之间有一些区别。

s = "  hello  "
print(s.rstrip())  # 输出:"  hello"
print(s.rstrip('o'))  # 输出:"  hello  "
s = "  hello  "
print(s.strip())  # 输出:"hello"
print(s.strip('h'))  # 输出:"  hello  "

总的来说,strip()函数会同时去除字符串的左右两侧指定字符,而rstrip()函数只去除字符串右侧的指定字符。

0
看了该问题的人还看了