Home  >  Article  >  Backend Development  >  Python how to check if a string is empty

Python how to check if a string is empty

silencement
silencementOriginal
2019-05-23 15:55:2319819browse

Checking method: 1. Use the len() function to check the length of the string. If the length is zero, it is empty, and the syntax is "len(string) ==0"; 2. Use isspace to determine whether the string is All are spaces, the syntax is "String.isspace()=True"; 3. Use the "String.strip()==''" code to judge.

Python how to check if a string is empty

#1. Use the string length to determine

len(s) ==0 then the string is empty

Python how to check if a string is empty

2. isspace determines whether all strings are spaces

s.isspace() == True

Python how to check if a string is empty

3. Characters Remove spaces and specified characters from the string. After removing the spaces to determine the length of the string, you can still determine whether the string is all spaces.

Python strip() method is used to remove specified characters at the beginning and end of the string (the default is spaces or newlines) or characters sequence.

Note: This method can only delete the beginning or end characters, but not the middle characters.

Remove spaces on both sides: str.strip()

Remove left spaces: str.lstrip()

Remove right spaces: str.rstrip()

Remove strings on both sides: str.strip('d'), correspondingly there are also lstrip, rstrip

Judgment method

if s.strip()=='':
print 's is null'

The above is the detailed content of Python how to check if a string is empty. 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