Sitemap

Operation on String

6 min readMay 25, 2025

--

String(str):-
— — — — — — — —
-> str is a pre-defined class which stores all types of primitive data types like int, float, bool, complex and can convert composite data types into str.
-> In a str, all data are stored within 2 single or double quotation or 3 single or double quotation.
-> It can store multi-line data within triple single or double quotes.
-> str is an immutable data types.
-> str accepts all inputs of a data as a single data.
Ex:-
a=’India’
print(a,type(a),len(a))
India <class ‘str’> 5

Operation on str:-
— — — — — — — — — — — — -
-> str supports indexing and slicing

1:indexing:-
— — — — — — — —
-> In main memory the data are stored with their unique position. The positions are denoted by integer which starts from 0 and end at (total no. data -1) position.
-> We can access each item individually by indicating their unique number. This is called indexing. The position value is called index value.
Ex:-
a=’India’
print(a[1])
n
print(a[3])
i

2:slicing:-
— — — — — —
-> As we discussed the data are stored with a unique position. We can access more than one element in a sequence.
-> There are 5 methods to return data with slicing.

Method:1-[start value:end value]
— — — — — — — —
-> It means we have to give a start index and an end index. The system will return the data from start index to (end index-1).
Ex:-
a=’india’
print(a[2:4])
di
print(a[0:3])
ind
print(a[0:6])
India

-> If the start index value is greater than end index. Then the output will be blank.
print(a[6:0])

print(‘-’*20)
— — — — — — — — — —
-> If the start index is less than 0, the start index is considered as 0. Like that if the end index is greater than total length, then the end index is considered as total length.
Ex:-
a=’India’
print(a[-100:3])
Ind
print(a[1:2000])
ndia
print(a[-1999:1999])
India

Method:2- [ :end value]
— — — — — — — -
-> If we return blank first value, then the system takes it as 0
Ex:-
a=’India’
print(a[ :3])
Ind
print(a[ :100])
India

Method:3- [start value: ]
— — — — — — — -
-> If we return blank end value, then the take it as total length.
Ex:-
a=’India’
print(a[1: ])
ndia
print(a[-100: ])
India

Method:4- [ : ]
— — — — — — — -
-> If we return blank both start and end value. Then the system will take 0 as start value and total length as end value. So it will give the whole data as the output.
Ex:-a=’India’
print(a[ : ])
India

Method:5- [start value:end value:step]
— — — — — — —
-> Start, end and step value may be positive, negetive or blank.
-> If the step is omitted, then system take it as 1.

Rule:1-
— — — — —
-> If the step value is positive, then the slicing will move forward direction upto (end-1) position.
Ex:-
a=’Python’
print(a[0:5:2])
Pto
print(a[ :4:3])
Ph
print(a[2:100:2])
to
print(a[-123:23: ])
Python

Rule:2-
— — — — -
->If the step value is negetive, PVM moves in backward direction.
-> It will move from end to (start-1).
-> Here the start value should be greater than end value.
->
Ex:-
a=’python’
print(a[ : :-1])
nohtyp
print(a[1:5:-1])

print(a[5:1:-1])
noht

Get Omprakasdash’s stories in your inbox

Join Medium for free to get updates from this writer.

3:capitalize():-
— — — — — — — — —
-> capitalize() function is used on str to capitalize the 1st letter of the data.
-> If the 1st letter is already capitalized, then it has no effect on the str data.
Ex:-
a=’india is a democratic country’
b=a.capitalize()
print(b)
‘India is a democratic country’
a.capitalize()
‘India is a democratic country’

4:title():-
— — — — — —
->In str, title() is used to capitalize the 1st letter of each word and converts the rest word to lowercase.
Ex:-
a=’india is a democratic country’
a.title()
‘India Is A Democratic Country’
a=’India is a Democratic Country’
a.title()
‘India Is A Democratic Country’
a= ‘India iS a deMoCratic Country’
a.title()
‘India Is A Democratic Country’

5:upper():-
— — — — — — —
-> upper() converts all small letter to capital letter.
Ex:-
a=’India is a democratic country’
a.upper()
‘INDIA IS A DEMOCRATIC COUNTRY’
a=’india IS a democratic country’
a.upper()
‘INDIA IS A DEMOCRATIC COUNTRY’
a=’65tg’
a.upper()
‘65TG’

6:lower:-
— — — — —
-> lower() function converts all capital letter to small letter.
Ex:-
a=’India is a democratic country’
a.lower()
‘india is a democratic country’
a=’India is a deMocratic cOuntry’
a.lower()
‘india is a democratic country’

7:index():-
— — — — — —
-> This function returns the the index of the spcified value.
-> If the specified value does not exist, the PVM will return ValueError.
-> If the existance of specified value is greater than 1, then the PVM will consider the 1st position.
Ex:-
a=’India is a democratic country’
a.index(‘d’)
2
a.index(‘s’)
7
a.index(‘ ‘)
5
a.index(‘I’)
0
a.index(‘i’)
3

8:isupper():-
— — — — — — —
-> In str, isupper() returns True, if the object data is in only upper case. Otherwise, it returns False.
Ex:-
a=’India is a democratic country’
a.isupper()
False
a.upper()
‘INDIA IS A DEMOCRATIC COUNTRY’
b=a.upper()
print(b)
INDIA IS A DEMOCRATIC COUNTRY
b.isupper()
True
a=’#@jhg’
a.isupper()
False

9:islower():-
— — — — — — —
-> In str, islower() returns True if all object data is in only lower case. Otherwise, it returns False.
Ex:-
a=’India is a democratic country’
a.islower()
False
a=’india is a democratic country’
a.islower()
True
a=’#@jhg’
a.islower()
True

10:isalpha():-
— — — — — — —
-> isalpha() returns True if the str object data is completely alphabet.
Ex:-
a=’india is a democratic country’
a.isalpha()
False
a=’India’
a.isalpha()
True

11:isdigit():-
— — — — — — —
-> isdigit() returns True if the str object data is completely whole number. Otherwise, it returns False.
Ex:-
a=”15th Aug 1947"
a.isdigit()
False
a=’1581947'
a.isdigit()
True
a=’3.5'
a.isdigit()
False

12:isalnum():-
— — — — — — — —
-> isalnum() returns True if the str obj data contains either alphabet or numerical value. Otherwise, it rerturns False.
Ex:-
a=’python3'
a.isalnum()
True
a=’python3.12'
a.isalnum()
False
a=’312'
a.isalnum()
True
a=’python’
a.isalnum()
True

13:isspace():-
— — — — — — —
-> isspace() returns True if the str object contains only space. Otherwise it returns False.
-> we can say isspace() returns True only on empty str.
Ex:-
a=’ ‘
a.isspace()
True
a=’Python Develpore’
a.isspace()
False

14:split():-
— — — — — —
-> It is used to break the data based on specific delimeter(-,@,$,%).
-> We can split it with perticular word or space.
-> We have to define perticular delimeter or word within the split() function.
-> If we don’t define any function, then by default it take space.
-> The output will be stored in list data type.
Ex:-
a=’Python@ Develpore’
a=’Python@ Develpore’
a.split(‘@’)
[‘Python’, ‘ Develpore’]
a=’Python@ Develpore’
a.split()
[‘Python@’, ‘Develpore’]
a.split(‘o’)
[‘Pyth’, ‘n@ Develp’, ‘re’]

15:-join():-
— — — — — —
-> join() is used to combine a number of interable object.
-> We can’t join more than 2 str at a time by using join().
Ex:-
a=’python’
b=’ ‘
a.join(b)
‘ ‘
b.join(a)
‘p y t h o n’

16:startswith():-
— — — — — — — — —
-> In startswith(), the user will return a value. The PVM will check is the statement is starts with that value or not.
-> If the value found at the beginning, then the PVM returns True otherwise returns False.
Ex:-
a=’python developer’
a.startswith(‘pyt’)
True
a.startswith(‘typ’)
False

17:-endswith():-
— — — — — — — — —
-> In endswith(), the user will return a value. The PVM will check weather the statement is ends with the given value or not.
-> If the value found at the end, then the PVM returns True otherwise returns False.
Ex:-
a=’python developer’
a.endswith(‘dev’)
False
a.endswith(‘per’)
True
a.endswith(‘develper’)
False
a.endswith(‘developer’)
True
a.endswith(‘developeR’)
False

18:-swapcase():-
— — — — — — — — —
-> swapcase() converts the lowercase letter to uppercase and vice versa.
Ex:-
a=’pYtyhOn DeVElopEr’
a.swapcase()
‘PyTYHoN dEveLOPeR’
a=’PYTHON DEVELOPER’
a.swapcase()
‘python developer’
a=’python developer’
a.swapcase()
‘PYTHON DEVELOPER’

--

--