Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7969089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:10:58+00:00 2026-06-04T07:10:58+00:00

Possible Duplicate: How to check if text is “empty” (spaces, tabs, newlines) in Python?

  • 0

Possible Duplicate:
How to check if text is “empty” (spaces, tabs, newlines) in Python?

I am trying to write a short function to process lines of text in a file. When it encounters a line with significant content (meaning more than just whitespace), it is to do something with that line. The control structure I wanted was

if '\S' in line: do something

or

if r'\S' in line: do something

(I tried the same combinations with double quotes also, and yes I had imported re.) The if statement above, in all the forms I tried, always returns False. In the end, I had to resort to the test

if re.search('\S', line) is not None: do something

This works, but it feels a little clumsy in relation to a simple if statement. My question, then, is why isn’t the if statement working, and is there a way to do something as (seemingly) elegant and simple?

I have another question unrelated to control structures, but since my suspicion is that it is also related to a possibly illegal use of regular expressions, I’ll ask it here. If I have a string

s = ” \t\tsome text \t \n\n”

The code

s.strip('\s')

returns the same string complete with spaces, tabs, and newlines (r’\s’ is no different). The code

s.strip()

returns “some text”. This, even though strip called with no character string supposedly defaults to stripping whitespace characters, which to my mind is exactly what the expression ‘\s’ is doing. Why is the one stripping whitespace and the other not?

Thanks for any clarification.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T07:10:59+00:00Added an answer on June 4, 2026 at 7:10 am

    Python string functions are not aware of regular expressions, so if you want to use them you have to use the re module.

    However if you are only interested in finding out of a string is entirely whitespace or not, you can use the str.isspace() function:

    >>> 'hello'.isspace()
    False
    >>> '  \n\t  '.isspace()
    True
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: How to check if a string is a legal “dd/mm/yyyy” date? I
Possible Duplicate: Faster DirectoryExists function? I want to check if some file exists on
Possible Duplicate: check what files are open in Python Hello, Is it possible to
Possible Duplicate: check number of a given character occurence in a String I'm trying
Possible Duplicates: Check if text is in a string JavaScript: string contains I'm trying
Possible Duplicate: Python - Check If Word Is In A String. i have a
Possible Duplicate: Check if multiple strings exist in another string I am trying to
Possible Duplicate: PHP strlen question how to check if text from post field is
Possible Duplicate: Check if a String Ends with a Number in PHP I'm trying
Possible Duplicate: Check if object is a jQuery object I need something like: function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.