Python 中的strip()函数

strip()函数是 Python 的预定义库函数。它用于通过移除传递给 strip()函数的前导和尾随空格、字符和符号来返回原始字符串的副本。换句话说,是 Python 字符串 strip()函数通过为 strip()函数指定一组字符作为参数来删除字符串左右两端的字符。默认情况下,如果没有参数传递给 strip()函数,它会从开始和结束字符串中删除空格。

句法


strip( 'characters' )

参数:

  1. strip(‘ chars ‘):strip()参数是可选参数。因此,如果程序员没有向 strip()函数传递任何参数,它将从字符串中删除开始和结束空格。
  2. 如果给定参数的集合被传递给 strip()函数,它将从原始字符串中移除字符或符号。

返回值:通过从原始字符串中删除字符集或空格,返回原始字符串的副本。

使用 strip()函数从给定字符串中删除字符或符号

让我们考虑一个在 Python 中通过移除给定字符串的前导或尾随字符来执行 strip()函数的示例。

剥离


strinput = "  $$$$$  No. 1 Welcome to JAVATPOINT!! No. 1 $$$ "   
# use strip() function to remove the set of characters
res = strinput.strip ( ' $No. 10 !' ) # store result into res variable
print ( " Given string is: ", strinput)
print ( " After removing the set of characters: ", res) 

str3 = ' 1 11 111 111 1111 Learn Python Programming Tutorial 1111 111 11 1 '
str4 = str3\. strip ( '1' )
print (" \n  Given string is ", str3)
print (" Stripping 1 from both ends of the string using strip ('1') function ", str4)

# define new string
str5 = '++++++Python Programming Tutorial****** $$$$$'
print ("\n Given string is = ", str5)
# use strip function to remove the symbols from both ends
str6 = str5\. strip ( ' $*+' )
print (" Stripping the '+', '*' and '$' symbols on both sides of the string is = ", str6)

输出

Given string is:    $$$$$  No. 1 Welcome to JAVATPOINT!! No. 1 $$$ 
 After removing the set of characters:  Welcome to JAVATPOINT

 Given string is   1 11 111 111 1111 Learn Python Programming Tutorial 1111 111 11 1
 Stripping 1 from both ends of the string using strip ('1') function   1 11 111 111 1111 Learn Python 
Programming Tutorial 1111 111 11 1

 Given string is =  ++++++Python Programming Tutorial****** $$$$$
 Stripping the '+', '*' and '$' symbols on both sides of the string is =  Python Programming Tutorial

使用 strip()函数删除给定字符串中的空白

让我们考虑一个在 Python 中通过移除给定字符串的前导或尾随空白来执行 strip()函数的示例。

条纹 2.py


str1 = '   Welcome to the World!              '
# print the original string
print (' Given string is: ', str1)

# use strip() function to remove whitespace
str2 = str1.strip()
print(" After removing whitespaces from an original string: ", str2)

str3 = '             Learn    Python    programming          '
# print the original string
print (' Given string is: ', str3)

# use strip() function to remove whitespace
str4 = str3.strip()
print(" After removing whitespaces from an original string: ", str4)

输出

Given string is:     Welcome to the World!
 After removing whitespaces from an original string:  Welcome to the World!
 Given string is:               Learn    Python    programming
 After removing whitespaces from an original string:  Learn    Python    programming

在上面的程序中,我们使用 strip()函数从给定字符串的开头和结尾移除空格,但它不会移除字符串之间的空格。

使用 strip()函数从用户处获取任何字符串并删除任何字符或符号的程序

程序


str1 = input( " Enter the string ") # take a string 
print (" Your string is ", str1) 
ch = input (" Enter any character or symbol that you don't want to see in the string ")
res = str1.strip (ch) 
# print the string after using a strip() method
print (" After performing the strip() function, \n Your string is ", res)

输出

Enter the string *** $$ Welcome to JavaTpoint. Learn Python programming !!! &&
 Your string is  *** $$ Welcome to JavaTpoint. Learn Python programming !!! &&
 Enter any character or symbol that you don't want to see in the string * $ Wel ! &
 After performing the strip() function, 
 Your string is  come to JavaTpoint. Learn Python programming

为什么使用 Python strip()函数?

下面是使用 python strip 函数的原因:

  1. 它有助于根据传递给 strip()函数的字符从原始字符串的开头和结尾移除字符。
  2. 如果用户没有向 strip 函数传递任何字符,默认情况下,它只删除字符串两端的空白。
  3. 如果原始字符串的开头或结尾没有空格,它将返回原始字符串而不进行修改。
  4. 如果传递的字符与原始字符串不匹配,strip 函数将返回原始字符串。

结论:

在上面的主题中,我们已经了解了 Python 库的 strip()函数。这是一个 strip()函数,用于从原始字符串的开头和结尾删除字符或空格。但是,如果用户没有向 strip()函数传递任何字符,它只会从主字符串的前导和尾部删除空格。


原创文章,作者:小蓝,如若转载,请注明出处:https://www.506064.com/n/293633.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
小蓝小蓝
上一篇 2024-12-26 13:14
下一篇 2024-12-26 13:14

相关推荐

  • Python中引入上一级目录中函数

    Python中经常需要调用其他文件夹中的模块或函数,其中一个常见的操作是引入上一级目录中的函数。在此,我们将从多个角度详细解释如何在Python中引入上一级目录的函数。 一、加入环…

    编程 2025-04-29
  • Python列表中负数的个数

    Python列表是一个有序的集合,可以存储多个不同类型的元素。而负数是指小于0的整数。在Python列表中,我们想要找到负数的个数,可以通过以下几个方面进行实现。 一、使用循环遍历…

    编程 2025-04-29
  • Python周杰伦代码用法介绍

    本文将从多个方面对Python周杰伦代码进行详细的阐述。 一、代码介绍 from urllib.request import urlopen from bs4 import Bea…

    编程 2025-04-29
  • 如何查看Anaconda中Python路径

    对Anaconda中Python路径即conda环境的查看进行详细的阐述。 一、使用命令行查看 1、在Windows系统中,可以使用命令提示符(cmd)或者Anaconda Pro…

    编程 2025-04-29
  • Python计算阳历日期对应周几

    本文介绍如何通过Python计算任意阳历日期对应周几。 一、获取日期 获取日期可以通过Python内置的模块datetime实现,示例代码如下: from datetime imp…

    编程 2025-04-29
  • Python程序需要编译才能执行

    Python 被广泛应用于数据分析、人工智能、科学计算等领域,它的灵活性和简单易学的性质使得越来越多的人喜欢使用 Python 进行编程。然而,在 Python 中程序执行的方式不…

    编程 2025-04-29
  • Python清华镜像下载

    Python清华镜像是一个高质量的Python开发资源镜像站,提供了Python及其相关的开发工具、框架和文档的下载服务。本文将从以下几个方面对Python清华镜像下载进行详细的阐…

    编程 2025-04-29
  • 蝴蝶优化算法Python版

    蝴蝶优化算法是一种基于仿生学的优化算法,模仿自然界中的蝴蝶进行搜索。它可以应用于多个领域的优化问题,包括数学优化、工程问题、机器学习等。本文将从多个方面对蝴蝶优化算法Python版…

    编程 2025-04-29
  • Python字典去重复工具

    使用Python语言编写字典去重复工具,可帮助用户快速去重复。 一、字典去重复工具的需求 在使用Python编写程序时,我们经常需要处理数据文件,其中包含了大量的重复数据。为了方便…

    编程 2025-04-29
  • python强行终止程序快捷键

    本文将从多个方面对python强行终止程序快捷键进行详细阐述,并提供相应代码示例。 一、Ctrl+C快捷键 Ctrl+C快捷键是在终端中经常用来强行终止运行的程序。当你在终端中运行…

    编程 2025-04-29

发表回复

登录后才能评论