Technology4 min read

Python String methods - isspace(), istitle(), and isidentifier()

Abhishek Ahlawat

By Abhishek Ahlawat — Recently

Founder of StudyTonight and a MERN fullstack developer. Writes about JavaScript, web development, Linux, and tech.

#Python#Python Strings

Summarize this article using AI

In this post, we will discuss string methods that identify whether a string contains a space, whether it is an identifier and whether it has title cased string in it. These methods are:

  1. isspace()

  2. istitle()

  3. isidentifier()

These methods are useful in building applications that require extensive string manipulation. Without further delay, let us dive into the details!


1. The isspace method

The isspace built-in method is used to check whether the string in question contains only whitespace characters or not. If the string contains only whitespace characters, it returns True and returns False otherwise. An empty string is considered to have no characters or whitespace, hence this method with return False.

Syntax of isspace method:

string.isspace()

It doesn't take any parameters and passing a parameter gives a TypeError. Passing the below-specified characters will return True using the isspace() method.

'\n' – Newline character
'\v' – Vertical tab
' ' – White space
'\t' – Horizontal tab
'\f' – Feed
'\r' – Carriage return

In the example below, we have checked one of the above characters, which returns True. Try the other characters on your own and see how things execute.

Time for an example:

string = 'StudyTonight'
print(string.isspace()) 
   
string = '\n \n \n'
print(string.isspace()) 
  
string = 'Study\n Tonight'
print( string.isspace()) 

# Two single quotes
string = ''
print(string.isspace())

string = '   '
print(string.isspace())

print(string.isspace('  '))

Output:

False
True
False
False
True
Traceback (most recent call last):
print(string.isspace('  '))
TypeError: isspace() takes no arguments (1 given)


2. The istitle method

The istitle built-in Python method is used to check if the characters in a string are title-cased. Title cased refers to characters in a string wherein the first letter of every word in a string is upper case and all the other characters in that string are lower case characters.

Syntax of istitle method:

string.istitle()

It doesn't take any parameters and returns TypeError if a parameter is passed to it. It returns True if the string is title-cased and False otherwise.

Time for an example:

string = 'Studytonight'
print(string.istitle()) 

string = 'Study Tonight'
print(string.istitle()) 

string = 'STudy tonight'
print(string.istitle()) 

string = 'Studytonight Contains 12 Characters '
print(string.istitle()) 

print(string.istitle('Studytonight'))

Output:

True
True
False
True
Traceback (most recent call last):
print(string.istitle('Studytonight'))
TypeError: istitle() takes no arguments (1 given)


3. The isidentifier method

As the name suggests, identifiers are used to give an identity to the variables, methods, classes and other entities that are used in building applications. There are certain rules to define an identifier. They are:

  1. It can contain lower case alphabets (a-z).

  2. It can contain upper case alphabets (A-Z).

  3. It can contain digits (0-9), but can't begin with a digit.

  4. It can contain an underscore (_), but can't begin with an underscore.

  5. Special characters like !, @, #, $, % can't be used as identifiers.

  6. It can be of any length.

This is a built-in method that helps identify if a string is a valid identifier or not. If an empty string is passed to this method, it returns False.

Syntax of isidentifier method:

string.isidentifier()

It doesn't take any parameters. The string refers to the string being checked for validity. This method returns a True if the string is a valid identifier and False otherwise.

Time for an example:

string = "StudyTonight"
print(string.isidentifier()) 

# Identifier that contains spaces
string = "Study Tonight"
print(string.isidentifier()) 

# An enpty string returns False when checked for being an identifier
string = ""
print(string.isidentifier()) 

# Alphanumeric data
string = "12StudyTonight"
print(string.isidentifier()) 

# Identifier that contains underscore
string = "Study_Tonight"
print(string.isidentifier()) 

# Identifier that begins with an underscore
string = "_Study_Tonight"
print(string.isidentifier())

Output:

True
False
False
False
True
True


Conclusion:

In this post, we saw string methods that help in identifying spaces, title case, and identifiers that could be present in a string. Work with the code, give different input strings and observe how the output changes.

More:

Python String: isdigit(), isnumeric() and isdecimel()

Python String: islower(), isupper() and isprintable()

Python String: isalpha(), isalnum() and isascii()

Python String: split(), join() and replace()

You may also like:

Spotted something outdated or wrong?

Liked this? See more StudyTonight posts in Google results.

Add StudyTonight as a preferred source
Next questLIVE OPSStep 2 / 3

Practice real incidents in the browser

System Design Ops — put out fires, ship fixes, and stack XP. Reading done. Time to play.

Play System Design
  • +XP

    Every sim

  • Incidents

    Live ops

  • Ranks

    Climb up

Read next