A Beginner Tutorial To Python String Method

In This Article You Will Learn About Python String Method Isspace(), istitle(), join(), just(), lower()

Python String Method – Before moving ahead, let’s know a little bit about methods Isdigit(), Isidentifier(), Islower(), Isnumeric() And Isprintable()

isspace() method – It  returns True if all the characters in a string are whitespaces, otherwise False.

Syntax - string.isspace()

Parameter Values - Parameter values.

Example 1- Check if all character contains whitespace.

x = ' '
y = x.isspace()

print(y)
python-string-method
As it is shown clearly that string is empty therefore, it returned True.

Example 2- Check if all character not found whitespace.

x = 'H'
y = x.isspace()

print(y)
python-string-method
As it is shown clearly that string is not empty therefore, it returned False.

istitle() method – It returns True if all words in a given string start with a upper-case letter, and the rest of the words are lower-case letters, otherwise False. It ignores symbols and numbers.

Syntax - string.istitle()

Parameter Values - No parameters.

Example 1- Check if each word starts with an upper-case letter –

x = 'Hello, Python World'
print(x.istitle())
python-string-method
As it is shown clearly that string started with upper-case letter ‘H’ therefore, it returned True.

Example 2- Check if each word starts with an upper-case letter –

x = 'PYTHON'
z = 'Python'
v = 'Hi 568'
c = 'Are, @, !'

print(x.istitle())
print(z.istitle())
print(v.istitle())
print(c.istitle())
python-string-method
As it is shown clearly it returned True and False respectively according to condition.

join() method – It returns string by joining all separated string of an element.

Syntax - string.join( iterable )

Parameter Values -

iterable - It is required. It contains iterable object which returned all the values in string.

Types of iterable - List, Tuple, String, Set and Dictionary.

Example 1- Using join() method with different iterable. In other words, adding string in tuple.

x = ('Hello', 'Python')
y = '**'
z = y.join(x)

print(z)
python-string-method
As it is shown clearly that it returned string after adding additional string.

Example 2- Adding string in list.

x = ['2', '6', '8', '9']
y = '%'
z = y.join(x)

print(z)
python-string-method
As it is shown clearly that it returned string after adding additional string.

Example 3- Adding string in set.

x = {'6', '8', '2', '9'}
y = '--'
z = y.join(x)

print(z)
python-string-method
As it is shown clearly that it returned string after adding additional string.

Note:  Order is random. A set is an unordered collection of items that’s why it will show different output every time.

Example 4- Adding string in dictionary.

x = {'name' : 'Alex', 'add' : 'NYC'}
y = '=='
z = y.join(x)

print(z)
python-string-method
As it is shown clearly that it returned string after adding additional string.

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

Ijust() method – It returns a left-justified string of a given minimum width.

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

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

Parameter Values -

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

fillchar - It used to fill remaining space of the width.

Parameter Values –

length - It is required argument. The length of the returned string.

character - It is optional argument. A character to fill the missing space (to the right of the string). Default is taken as " " (space).

Example 1- Left justify string of minimum width.

x = 'python'
z = 10

print(x.ljust(z))
python-string-method
As it is shown clearly that by default string is filled with whitespaces.

Example 2- Left justify string and fill the remaining spaces.

x = 'python'
z = 10
y = '<'

print(x.Ijust(z, y))
python-string-method
As it is shown clearly that by default string is filled with arrow.

lower() method – It returns given string in lower case.  It ignores Symbols and Numbers.

Syntax - string.lower()

Parameter Values - No parameter values.

Example – Convert all character of given string in lower-case.

x = 'HELLO! PYTHON'
print(x.lower())
python-string-method
As it is shown clearly that string returned in lower case after converting into lower case.

If you find anything incorrect in above discussed topic and have any further question, please comment down below.

Like us on

1 thought on “A Beginner Tutorial To Python String Method”

Leave a Comment

Stay in the loop

codingstreets