Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Python String format_map() Method
Next article icon

isspace() method in Python is used to check if all characters in astring are whitespace characters. This includes spaces (' '), tabs (\t), newlines (\n), and other Unicode-defined whitespace characters. This method is particularly helpful when validating input or processing text to ensure that it contains only whitespace characters.

Let's understand this with the help of an example:

Python
s="   "print(s.isspace())

Output
True

Explanation:

  • The string" " contains only whitespace characters (spaces).
  • Theisspace() method returnsTrue since all characters in the string are whitespace.

Syntax of isspace() method

string.isspace()

Parameters

  • The isspace() method does not take any parameters.

Return Type

  • ReturnsTrue if all characters in the string are whitespace.
  • ReturnsFalse if the string contains one or more non-whitespace characters or is empty.

String with mixed characters

Let's see what happens when the string contains non-whitespace characters:

Python
s="Python is fun! "print(s.isspace())

Output
False

Explanation:

  • The string contains non-whitespace characters ("Python is fun!").
  • The presence of these characters causes the method to returnFalse.

String with tabs and newlines

isspace() method considers tabs and newlines as whitespace characters:

Python
s="\t\n"print(s.isspace())

Output
True

Explanation:

  • The string contains only a tab (\t) and a newline (\n), both of which are valid whitespace characters.
  • The method returnsTrue.

What happens when the string is empty?

An empty string does not contain any characters, including whitespace.

Python
s=""print(s.isspace())

Output
False

Explanation:

  • The method returns False.

Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp