DEV Community

Anirban Das
Anirban Das

Posted on

Python String Methods

Python has a set of built-in methods that you can use on strings.
All string methods returns new values. They do not change the original string.

1) capitalize()

The capitalize() converts the first character to upper case.

txt = "hello, and welcome to my world."
x = txt.capitalize()
print(x) # Hello, and welcome to my world.
Enter fullscreen mode Exit fullscreen mode

2) count(value)

The count() method returns the number of times a specified value appears in the string.

txt = "I love apples, apple are my favorite fruit"
x = txt.count("apple")
print(x) # 2
Enter fullscreen mode Exit fullscreen mode

3) find(value)

Searches the string for a specified value and returns the position of where it was found.

The find() method finds the first occurrence of the specified value.
It returns -1 if the value is not found.

txt = "Hello, welcome to my world."
x = txt.find("welcome")
print(x) # 7
Enter fullscreen mode Exit fullscreen mode

4) String Slicing

To obtain a substring from a given string we use slicing.

We need to specify the start index and the end index seperated by a colon.

name = "Hey, This is Anirban"
print(name[13:20]) # Anirban
print(name[13:]) # Anirban
print(name[-7:]) # Anirban
Enter fullscreen mode Exit fullscreen mode

5) replace(oldvalue, newvalue)

The replace() method returns a string where a specified value is replaced with another value given.

txt = "I like bananas"
x = txt.replace("bananas", "apples")
print(x) # I like apples
Enter fullscreen mode Exit fullscreen mode

6) strip(characters)

The strip() method removes any whitespaces at the beginning and ending of a string and returns the trimed string.

Optionally we can even give a character to be removed from the string.

txt = "   Mount Blue   "
x = txt.strip()
print(x) # Mount Blue
Enter fullscreen mode Exit fullscreen mode

7) upper()

The upper() converts a string into uppercase

txt = "Hello my friends"
x = txt.upper()
print(x) # HELLO MY FRIENDS
Enter fullscreen mode Exit fullscreen mode

8) lower()

The lower() converts a strubg into lowercase

txt = "Hello my FRIENDS"
x = txt.lower()
print(x) # hello my friends
Enter fullscreen mode Exit fullscreen mode

9) join(iterable)

The join() converts the elements of an iterable into a string

my_tuple = ("John", "Anirban", "Roshan")
x = ' '.join(my_tuple)
print(x) # John Anirban Roshan
Enter fullscreen mode Exit fullscreen mode

10) swapcase()

The swapcase() swap the case of the elements of a string from upper to lower and vice versa.

name = "Anirban"
x = name.swapcase()
print(x) # aNIRBAN
Enter fullscreen mode Exit fullscreen mode

11) islower() and isupper()

islower() returns true if all the characters in the string are in lowercase

isupper() returns true if all the characters in the string are in uppercase

name = "anirban"
print(name.islower()) # True
print(name.isupper()) # False
Enter fullscreen mode Exit fullscreen mode

12) isalnum(), isalpha() and isdigit()

isalnum() returns true if all the characters of the string are alphanumeric in nature

isalpha() returns true if all the characters of the string are alphabetic in nature

isdigit() returns true if all the characters of the string are digits

name = "Company12"
print(name.isalnum()) # True
print(name.isalpha()) # False
print(name.isdigit()) # False
Enter fullscreen mode Exit fullscreen mode

13) title()

The title() converts the first character of each word to upper case

txt = "Welcome to my world"
x = txt.title()
print(x) # Welcome To My World
Enter fullscreen mode Exit fullscreen mode

References:

Top comments (0)