In this post, we will learn about String methods in python with very basic examples and easy explanations before we jump into string methods we should know about what is string in python.

What is string in python?

In general, the String is a sequence of characters but in python, you can say that anything (characters, numbers, symbols, etc) inside single or double quotes ( ‘ ___’ or “___” ) is considered a string.

For Example —> str=“Allinpython”

Now let’s talk about string methods in python.

String methods

String methods are used to manipulate strings however, strings are immutable but string methods create a new copy of the existing string with few changes according to string methods.

there are many string methods used to manipulate strings let’s see all the methods one by one.

.upper()

upper(): upper method is used to change a string into uppercase.

Syntex –> str.upper()

Example:

string = "allinpython"
string.upper() 
print(string) 

# above example will not work because string methods 
# always create new copy of existing string 
# you should store in new variable or print as shown below.
  
print("String in uppercase:",string.upper())

Output:

allinpython
String in uppercase: ALLINPYTHON

.lower()

lower(): lower method is used to change a string into lowercase.

Syntex –> str.lower()

Example:

myString = 'THIS IS LEARNING WEBSITES'
print(myString)
# myString in lowercase
print(myString.lower())

# OR

str="ALLINPYTHON"
print(str)
print(str.lower())

Output:

THIS IS LEARNING WEBSITES
this is learning websites
ALLINPYTHON
allinpython

.capitalize()

capitalize(): capitalize method is used to change the first characters of the string into uppercase.

Syntex –> str.capitalize()

Example:

mystring2 = "this is demo"
print(mystring2)
print(mystring2.capitalize())

# OR

str="allinpython!"
print(str)
print(str.capitalize())  # First letter is in capital

Output:

this is demo
This is demo
allinpython!
Allinpython!

.title()

title(): title method is used to change the first letter of every word in the string into uppercase.

Syntex –> str.title()

Example:

# title
mystring2 = "this is title methods"
print(mystring2)
print(mystring2.title()) 

Output:

this is title methods
This Is Title Methods

.count()

count(): The count() method returns the number of times specified characters or words appear in the string.

Syntex –> str.count(substring)

Example:

# conut method

str = "my name is allinpython"
print(str.count("allinpython"))
print(str.count("a"))

Output:

1
2

.find()

find(): find method is finding the index of the first occurrence of a  specified element in a string.

Syntex –> str.find(substring)

NOTE: find method return -1 if specified value is not present in string.

Example:

# find method

str = "am learning programing language"
print(str.find("learning"))
print(str.find("language"))
print(str.find("I"))

Output:

3
23
-1

.replace()

replace(): replace method replaces current words or characters with new words or characters.

Syntex –> str.replace(current string, new string)

Example:

mystring = "allinpython.com"

print(mystring.replace("a",'A'))
print(mystring.replace(".",'*'))

Output:

Allinpython.com
allinpython*com

.join()

join(): join method join all the items of iterable (list, tuples, dictionary, etc. are known as iterable) into a single string with the help of a separator.

Syntex –> separator.join(iterables)

Example:

name1 = ['ibrahim', '21','saurabh','20']
print(','.join(name1))

myitr = ('ram','syam','krish','khusi')
print('*'.join(myitr))

name2 = {'name':'yag','address':'allinpython.com'}
print('_'.join(name2))

Output:

ibrahim,21,saurabh,20
ram*syam*krish*khusi
name_address

.split()

split(): The split() method is used to split a string into a list of substrings based on a specified separator. By default, the separator is whitespace.

Syntex –> string.split(separator, maxsplit)

  • string: the string you want to split.
  • separator (optional): the character or string used to separate the substrings in the original string. If not specified, any whitespace character (e.g. space, tab, newline) is used as the separator.
  • maxsplit (optional): the maximum number of splits that should be performed. If not specified, all possible splits are performed.

Example:

# Split a string based on a space separator
text = "Hello world"
words = text.split()
print(words)   # Output: ['Hello', 'world']

# Split a string based on a comma separator
csv = "apple,orange,banana"
fruits = csv.split(",")
print(fruits)   # Output: ['apple', 'orange', 'banana']

# Split a string into three substrings at most
text = "The quick brown fox jumps over the lazy dog"
words = text.split(" ", 3)
print(words)   # Output: ['The', 'quick', 'brown', 'fox jumps over the lazy dog']

.isalnum()

isalnum() :  isalnum method return true if string contains only letter’s or numbers otherwise it return false.

Syntex –> str.isalnum()

Example:

str="allinpython12"    
print(str.isalnum())  # True

str="allinpython**1223"
print(str.isalnum()) # False

# OR

mystring = "123"
print(mystring.isalnum())  # True

mystring = "123 this is "
print(mystring.isalnum()) # False

# space is also consider as character
# thats why it return false 

Output:

True
False
True
False

.isdigit()

isdigit() : isdigit method is return true if string contains only numbers (digit) otherwise return false.

Syntex –> str.isdigit()

Example:

string = "123"
print(string.isdigit())

string1 = "allinpython123"
print(string1.isdigit())

Output:

True
False

.islower()

islower() : islower method return true if all the letters of string is in lowercase otherwise it return false.

Syntex –> str.islower()

Example:

mystring3 = "this is osm"
print(mystring3.islower())

mystring4 = "THIS IS MINE"
print(mystring4.islower())

# NOTE: if any one charecter is in uppercase then islower() method return false.

# Example:

mystring5 = "This is demo"
print(mystring5.islower())

Output:

True
False
False

.isupper()

isupper() : isupper method return true if all the letters of string is in uppercase otherwise it return false.

Syntex –> str.isupper()

Example:

str = "ALL in python"
print(str.isupper())

# NOTE: if any one charecter is in lowercase then isupper() method return false.

str1 = "ALL IN PYTHON"
print(str1.isupper())

Output:

False
True

.isnumeric()

isnumeric() : isnumeric method is returns true if a string contains only numeric characters otherwise return false.

Syntex –> str.isnumeric()

Example:

str = "15081947"
print(str.isnumeric())

str1 = "this is number 1"
print(str1.isnumeric())

Output:

True
False

.istitle()

istitle() : istitle method returns true if string is properly “titlecased” otherwise it return false.

Syntex –> str.istitle()

Example:

# istitle
string = "all in python"
print(string.istitle())

string1 = "All In Python"
print(string1.istitle())

Output:

False
True

There are a few more string methods that are rarely used in python programs and they are listed below

lstrip() rstrip()strip()
center()isspace()maketrans()

Hope this post add some good value to your life -thankyou

Author

Hi, I'm Yagyavendra Tiwari, a computer engineer with a strong passion for programming. I'm excited to share my programming knowledge with everyone here and help educate others in this field.

Write A Comment