String Methods in Python: Part-1

Hello, readers. I am going to explain some important string methods in Python in this multi-part series.

I had given some information about strings in my post on Data Types, along with Concatenation, Slicing and Repetition. So, I am directly going to start with the string methods.

Lets begin!

1. lower() and upper()-

The lower() function converts a given string into lowercase. In other words, this function converts all the characters of a string into 'small' or 'non-capital' letters.

The upper() function does exactly the opposite. It converts all characters of the given string into uppercase or 'capital' letters.

For example-

string=("Hello World!")

print("The lowercase string is", string.lower())
print("The uppercase string is", string.upper())

The output;

The lowercase string is hello world!
The uppercase string is HELLO WORLD!

The () or parentheses are used to call the function and the function will not provide an output unless you call it.

2. split()-

This is another useful function. The split() function divides the string into substrings based on the rule given. If the parentheses are left empty, it will divide the string based on the location of the whitespaces.

Let us look at this example;

string=("Apple is a fruit.")
a=string.split()

print(a)

The output will be;

['Apple', 'is', 'a', 'fruit.']

3. isalpha(), isnumeric() and isalnum()

The isalpha() checks if all the characters in the input string are alphabets. It returns True if they are alphabets and False if it is not.

The isnumeric() function does the same but with numbers. Alternatively, you can use isdigit(), which has the same functionality.

However, the isalnum() returns the same if all the elements of the list are either alphabets or numbers.

The following example shows the use of these functions;

string1=("Do you know about Texplainers")
string2=("1543454")
string3=("Hello 123")

print(string1.isalpha())
print(string2.isnumeric())
print(string2.isdigit())
print(string3.isalnum())

The output will show;

True
True
True


That is all I have for this post. You can find more string methods here.

Please feel free to comment your opinions down below. I will definitely take the time out to read them.

Cheers,

Aarav Iyer

Aarav Iyer

I am a technology and programming enthusiast, currently a high school student. I love drawing and am quite interested in aeronautics and astrophysics too. My favourite pastimes are reading books, blogging and skywatching with my telescope.

3 Comments

Next Post Previous Post