python-string-methods-isspace-istitle-join-ijust-lower

Python String Methods isspace() istitle() join() Ijust() lower()

In this article, learn about important Python string methods such as isspace(), istitle(), join(), Ijust(), and lower(). Discover how to check if a string consists of only whitespace characters, determine if a string is titlecased, concatenate strings using the join() method, capitalize the first letter of a string, and convert a string to lowercase. Get ready to enhance your Python string manipulation skills.

Before moving ahead, let’s see Python String Methods.

Table of Contents

isspace() method

isspace() returns True if all characters are whitespace in the string, else returns False.

Syntax – string.isspace()

Parameter Values – No

Example: Check if all characters are whitespace in the string.

				
					text = ' '
is_space = text.isspace()

print(is_space)
 
#output:
True
				
			

Example: Check if all characters are whitespace in the string.

				
					text = 'Hello'
is_space = text.isspace()

print(is_space)
 
#output:
False
				
			

istitle() method

istitle() returns True, if all words with the first letter having in upper-case and the rest are in lower-case, else returns False. It ignores the symbol and numbers.

Syntax – string.istitle()

Parameter Values – No

Example: Check if only the first letter of each word is in upper-case.   

				
					text = 'Hello World'
is_title = text.istitle()

print(is_title)

#output:
True
				
			

Example: Check if only the first letter of each word is in upper-case.

				
					text = 'Hello world'
is_title = text.istitle()

print(is_title)

#output:
False
				
			

Example: Check if only the first letter of each word is in upper-case.

				
					text1 = 'PYTHON'
text2 = 'Python'
text3 = 'Hi 568'
text4 = 'Are, @, !'

print(text1.istitle())
print(text2.istitle())
print(text3.istitle())
print(text4.istitle())
				
			

join() method

join() returns the string after joining all separated strings with the specified values. 

Syntax – string.join( iterable )

Parameter Values –

iterable – Required. Contains an iterable object which returned all the values in a string.

Types of iterable – List, Tuple, String, Set, and Dictionary

Example: Join the string by ‘**’. 

				
					text = 'name', 'Alex'
text_join = '='
print(text_join.join(text))

#output:
name=Alex
				
			

Example: Join the Tuple string by ‘**’.

				
					text = ('Hello', 'World')
text_join = '**'
print(text_join.join(text))

#output:
Hello**World
				
			

Example: Join the List of strings by ‘**’.

				
					text = ['1', '2', '3']
text_join = '**'
print(text_join.join(text))

#output:
1**2**3
				
			

Example: Join the Set of strings by ‘$’.

				
					text = {'1', '2', '3'}
text_join = '$'
print(text_join.join(text))


#output:
1$3$2
				
			

Example: Join the Set of strings by ‘=’.

				
					text = {'name' : 'Alex', 'add' : 'NYC'}
text_join = '='
print(text_join.join(text))

#output:
name=add
				
			

Note:  A dictionary as an iterable, returned only the keys not values.

Ijust() method

Ijust() returns a left-justified string within a minimum width.

Syntax – string.ljust(width[, fillchar])

or simply it can be string.ljust( length, character )

Parameter Values –

width – If the width is less than or equal to the length of the string then it returns the original string.

fillchar – fillchar is used to fill the remaining space of the width.

Parameter Values –

length – Required. Returns the length of the string.

character – Optional. A character to fill the missing space (to the right of the string). Default is taken as ” ” (whitespace).

Example: Returns the left-justified string, filled with space.

				
					text = 'Python'
width = 10
print(text.ljust(width))

#output:
Python    
				
			

Explanation: Width > string length. Returns the left-justified string filled with whitespace.

Example: Returns the left-justified string.

				
					text = 'Python'
width = 5
print(text.ljust(width))

#output:
Python
				
			

Explanation: Width < string length, therefore returns the original string.

Example: Returns the left-justified string.

				
					text = 'Python'
width = 6
print(text.ljust(width))

#output:
Python
				
			

Explanation: String length = width, therefore returns the original string.

				
					text = 'Python'
width = 10
character = '^'

print(text.ljust(width, character))

#output:
Python^^^^

				
			

lower() method

lower() returns the given string in lowercase. It ignores Symbols and Numbers.

Syntax – string.lower()

Parameter Values – No

Example: Convert all characters of the given string in lowercase.

				
					text = 'HELLO! PYTHON'
print(text.lower())

#output:
hello! python
				
			

Example: Convert all characters of the given string in lowercase.

				
					text = 'Hello! Python'
print(text.lower())

#output:
hello! python
				
			

Conclusion

This article provides an in-depth exploration of several important Python string methods: isspace(), istitle(), join(), Ijust(), and lower(). These methods serve various purposes in string manipulation and can greatly enhance your Python programming skills.

By understanding and effectively utilizing these Python string methods, you’ll have the tools to manipulate strings, check for specific conditions, and format your output according to your needs.

Recent Post

Popular Post

Top Articles

Archives
Categories

Share on