Python 中的 Strip() 函数

介绍

strip() 是一个内置的 Python 函数,用于去除参数中指定字符的字符串,并打印出不带指定字符的字符串。

它是快速从字符串或文本中删除不需要的字符的有用工具。 这是用户执行此操作的明智方法。 现在我们将开始向您介绍如何在 Python 中使用 strip() 函数。 希望你能理解。

例子

str = ",,,,,,....cat...,,,"

x = str.strip(",.")

print(x)

输出:

定义

strip() 函数打印出与原始字符串相同的字符串,并删除参数中指定的字符。

语法

string.strip(字符)

参数值:

characters:要删除的字符

更多例子

示例 1: 删除空格字符

str = " cat "

x = str.strip()

print(x)

输出:

示例 2:

# Don't use strip()

str1 = "Linux for people"

print(str1)

# Use strip()

str2 = "for people"

print(str1.strip(str2))

输出:

Linux 为人

Linux

结论

我们向您介绍了如何在 Python 中使用 strip() 函数。

感谢您的阅读!