MediumThe complete guide to python strings. | by Narrasairam | Medium
Sitemap

The complete guide to python strings.

10 min readJul 19, 2026

--

Python String Methods

String is a collection sequence of characters or empty or single character enclosed within a pair of single(‘ ’) or double(“ ”) or triple quotes(“‘ ”’). characters include alphabets, digits and special symbols. Strings are immutable once created can’t be modified like add or replace or delete, but we can perform only operation on string using String methods.

String Methods
1. upper()
upper() is a string method, which is used to convert a only a lower case into upper case characters in a string remaining elements will same, and returns in a new string.

# converted each lowercase to uppercase
# converted each lowercase to uppercase, remaining characters return same

2.lower()
lower() is a string method, which is used to convert only upper case into lower case characters in a string remaining elements will same, and returns in a new string.

# converted each uppercase to lowercase elements
# converted each uppercase to lowercase elements remaining characters return same

3. swapcase()
swapcase() is a string method, which is used to convert the lowercase characters to uppercase character vice versa, remaining elements will same, and returns in a new string.

# converted each uppercase to lowercase elements vice versa
# converted each uppercase to lowercase elements vice versa, remaining characters return same

4. capitalize()
capitalize() is a string method, which is used to convert only first character in a string into uppercase and remaining characters to lowercase except digits and special symbols.

# converted only first element into uppercase, reaming to lowercase elements.
# converted only first element into uppercase, reaming to lowercase elements except digits and special symbols

5.title()
title() is a string method, which is used to covert every first element in each word into uppercase and all remaining into lowercase, except digits and special symbols in a string, words are separated by special symbol or digits.

converted first element in each word into uppercae and remaining into lowercase
converted first element in each word into uppercae and remaining into lowercase except digits and special symbols,

6.count()
count() is string method, which is used to count the occurrence of characters or elements in a string.
In a count() — > must pass at least one argument in a count, else throughs an runtime error called type error.

→ In count() if not specifies the element within in a pair of quotes, it prints entire string.

→ In example 3,the argument that i passed a space , space a special symbol. count() counts the spaces and return total number of spaces that occurred in a string

In example 4, i passed an argument ‘a’ in count(), that returns the total number of occurrence of ‘a’ in a give string.

In example 5, i passed an argument ‘am’ in count(), that returns the total number of occurrence of ‘am’ in a give string.

In example 6, i passed an argument ‘a’ in count() method, as starting index with positive value (0) and ending index with positive value (15) , it counts ‘a’ from left to right and returns the total number of occurrence of ‘a’ in the given string.

In example 7, i passed an argument ‘a’ in count() with start index 0 and end index 15, it counts ‘a’ from left to right and returns the total number of occurrence of ‘a’ in a given string.

7.index()
Index() is a string method, which is used to find the first occurrence of the substring index position, the sub string searches left to right.

Press enter or click to view image in full size

8.rindex()

Index() is a string method, which is used to find the first occurrence of the substring index position, the sub string searches from right to left

9.find()
find() is a string method, which is used to find the first occurrence of the substring index position, the sub string searches from left to right.

10.rfind()
rfind() is a string method, which is used to find the first occurrence of the substring index position, the sub string searches from right to left.

11.replace()
replace() is a string method, which is used to replace a sub-string with another sub-string

12.strip()
strip() is a string method, which is used to remove the spaces from a string at only left end and right end.

13.lstrip()

strip() is a string method, which is used to remove the spaces from a string at only left end.

14.rstrip()
strip() is a string method, which is used to remove the spaces from a string at only right end.

15.split()
split() is a string method, which is used to divide the string into multiple substrings and returns in a list

16.rsplit()

Get Narrasairam’s stories in your inbox

Join Medium for free to get updates from this writer.

rsplit() is a string method, which is used to divide the string from right end into multiple substrings and returns in a list

17.partition()
partition() is a string method, which is used to divide the string into multiple sub-string with include argument substring and returns in a tuple.

18.center()
center() is a string method, which is used to make the string at center with spaces default if the length of string is more than the argument if not returns same string.

Press enter or click to view image in full size

19.zfill()
zfill() is a string method, which is used to fill zeros before the string

20.maketrans()
maketrans() is a string method, which is used to create a mapping table

21.translate()
translate() is a string method, which is used to translate the mapped table

22.isupper()
isupper() is a string method, which is used to check the string contain all character in capital or not, it returns the boolean value.

23.islower()
islower() is a string method, which is used to check the string contain all characters in lowercase or not, it returns the boolean value.

24.istitle()
istitle() is a string method, which is used to check whether each word capital letter in a string, it returns a boolean value.

25.isalpha()
isalpha() is a string method, which is used to check whether the string contain only alphabets or not, it returns the boolean value

26.isdigit()
isdigit() is a string method, which is used to check whether the stirng contain only digits from 0–9 or not, it returns the boolean value.

27.isalnum()
isalnum() is a string method, which is used to check the string contain only alphabets and numbers without any special characters, it returns the boolean value

28.isspace()
isspace() is a string method, which is used to check whether a string is only with spaces or not, it returns the boolen value

29.isidentifier()
isidentifier() is a string method, which is used to indentify wheteher the string forms the valid variable or not, it returns the boolen value

30.startswith()
startswith() is a string method, which is used to check the string is startswith the arguments or not, it returns the boolean value

#30 startswith()
var= 'abcd'
var10.startswith('a')

#output
True

31.endswith()
endswith() is a string method, which is used to check the string is endswith the arguments or not, it returns the boolean value

#31 endswith()
var= 'abcd'
var10.endswith('a')

#output
False

32.format()
format() is a string method which is used to combine srting data with another data with the help of place holders.

a=2
b=3
print('sum of {} and {} is {}'.format(a,b,a+b)

output
#sum of 2 and 3 is 5

33.input()
input() is string method, which is used to take the input , display the message written in input and returns in a string.

var= input('Enter a string :')

#output
Enter a string :

--

--