Home  >  Article  >  Backend Development  >  How to use the strip() function to remove spaces at both ends of a string in Python 3.x

How to use the strip() function to remove spaces at both ends of a string in Python 3.x

王林
王林Original
2023-08-01 14:14:041370browse

How to use the strip() function to remove spaces at both ends of a string in Python 3.x

In Python programming, strings are one of the commonly used data types. It is often encountered that strings contain spaces. For processing such strings, we can use Python's built-in strip() function to remove spaces at both ends of the string. This article will introduce how to use the strip() function and provide corresponding sample code.

Syntax:
The strip() function is a method of Python string object, which can be used to remove spaces at both ends of the string.

str.strip([char])

Among them, str represents the string object to be processed, and char (optional) represents the characters to be removed.

Sample code:

# 示例1:去除字符串两端的空格
str1 = "  Hello, World!  "
str2 = str1.strip()
print("处理前的字符串:", str1)
print("处理后的字符串:", str2)

# 示例2:去除特定字符
str3 = "||||Hello, World!||||"
str4 = str3.strip('|')
print("处理前的字符串:", str3)
print("处理后的字符串:", str4)

Running result:

处理前的字符串:   Hello, World!  
处理后的字符串: Hello, World!
处理前的字符串: ||||Hello, World!||||
处理后的字符串: Hello, World!

From the above sample code, we can see how to use the strip() function. When no parameters are passed in, the strip() function will remove spaces at both ends of the string by default. When specified characters are passed in, the strip() function will remove these specified characters appearing at both ends of the string.

It should be noted that the strip() function does not modify the original string, but returns a new string. Therefore, we need to assign the return value to another variable to save the processed result.

In addition, we can also use the lstrip() function and rstrip() function to remove spaces from the left and right ends of the string respectively. The usage is similar to the strip() function. For details, please refer to the following sample code:

# 示例3:去除字符串左端的空格
str5 = "  Hello, World!  "
str6 = str5.lstrip()
print("处理前的字符串:", str5)
print("处理后的字符串:", str6)

# 示例4:去除字符串右端的空格
str7 = "  Hello, World!  "
str8 = str7.rstrip()
print("处理前的字符串:", str7)
print("处理后的字符串:", str8)

Running results:

处理前的字符串:   Hello, World!  
处理后的字符串: Hello, World!  
处理前的字符串:   Hello, World!  
处理后的字符串:   Hello, World!

The above is the method and sample code for using the strip() function to remove spaces at both ends of a string. I hope this article can help readers better understand and use the strip() function in Python.

The above is the detailed content of How to use the strip() function to remove spaces at both ends of a string in Python 3.x. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn