Python - Datatype - String Tutorial
The String is formed using multiple Unicode characters. It can be used within a single quote ' ' or double quote " " or single triple quote ''' ''' or double triple quote """ """.
The string is represented as arrays of bytes, indexing is used to access the element of the string.
Python does not have a char data type. A character is referred to as a string with a length of 1.
a = "Welcome to Python" #for single line we can use '' or ""
print(a)
a = '''Welcome to
Fresherbell''' #for multi line we can use ''' or """
print(a)
Output:
Welcome to Python
Welcome to
Fresherbell
The String is immutable. Therefore we cannot change the value of the string once it's declared.
a = "Welcome to Fresherbell"
a[0]="D"
print(a)
Output:
a[0]="D"
TypeError: 'str' object does not support item assignment
Accessing Character Of a String
The individual character of a string can be accessed by using the method of indexing. Indexing start from 0 to len(str) i.e length of string.
Indexing also allows negative indexing, where -1 refers to the last index, -2 refers last-second index, and so on.
Accessing the index out of range will give an error - IndexError. An only integer is used for indexing, float and other data type give an error - TypeError
a="Fresherbell"
print(a)
print(a[0])
print(a[-2])
print(a[14])
print(a[1.00])
Output:-
Fresherbell
F
l
IndexError: string index out of range
TypeError: string indices must be integers
Slicing Operator ( : )
We can also use the slicing operator [] in the string, to access a range of particular characters.
Syntax:-
str[start index, stop index, step size]
str refers to a string
When Step Size is +ve, then start index is 0 and stop index is len(a) i.e var[::1]
When Step Size is -ve, then start index is -1 and stop index is -len(a) i.e var[::-1]
Example-
a = "Fresherbell"
print(len(a))
#If indexing of a[0:11],then the string that
can be accessible is 0 to 10 indexing
print(a[0:len(a)])
#It will access string from 7 to last char
of string, step-index is 1 by default
print(a[7:])
#When Step Size is -ve,
#then start index is -1 and stop index is -len(a) i.e var[::-1]
#When Step Size is +ve
#then start index is 0 and stop index is len(a) i.e var[::1]
print(a[::-1])
print(a[::1])
#It will access string in reverse direction
#start stop index(-ve) and step index(-ve)
#both have same direction, hence, no empty string
print(a[-1:-7:-1])
#It will access particular string in reverse direction
#start stop index(+ve) and step index(+ve)
#both have same direction, hence, no empty string
print(a[-4:-2:1])
#Direction of start stop index - Left to Right
#Direction of step index - Right to Left
#Direction is different. Hence, it will provide empty string
print("Empty String 1")
print(a[0:11:-1])#By default step index is 1
print("Empty String 1 End")
#Direction of start stop index - Right to Left
#Direction of step index - Left to Right
#Direction is different. Hence, it will provide empty string
print("Empty String 2")
print(a[-1:-len(a)-1])#By default step index is 1
print(a[-3:0:1])
print("Empty String 2 End")
Output:
11
Fresherbell
bell
llebrehserF
Fresherbell
llebre
be
Empty String 1
Empty String 1 End
Empty String 2
Empty String 2 End
Operations On String
We can perform many operations on strings without using any method.
- Concatenation - It is as simple as joining more than one string using the + operator. both the operand should be a string.
a = "Fresherbell"
b = "Tutorial"
print(a +" "+ b)
Output:-
Fresherbell Tutorial
- Repetition - repeating the string using the * operator. one operand should be a string and the other should be int.
a = "Fresherbell"
print(a * 3)
Output:-
FresherbellFresherbellFresherbell
- Membership - It will check the existence of a character or substring in a string. Operators are in and not in. It will return a boolean value.
a = "Fresherbell"
print("bell" in a)
print("python" in a)
Output:-
True
False
- Identity Operator - It will check if the memory address of two variables is the same or not. Operators are is and is not. It will return a boolean value.
#Example1
print("Example1")
a = "This is Python tutorial"
b = "This is Python tutorial"
print(a is b)
print(a is not b)
print(id(a))
print(id(b))
#Example2
print("Example2")
c = "ThisisPythontutorial"
d = "ThisisPythontutorial"
print(c is d)
print(c is not d)
print(id(c))
print(id(d))
Output:-
Example1
False
True
2440511239872
2440519721168
Example2
True
False
2440519027488
2440519027488
For Example1, the reason is A-Z, a-z, 0-9, _ is applicable in String, but space is not applicable, because of space both id of a and b are different. Hence, if we print(a is b), the result is False.
For Example2, the reason is A-Z, a-z, 0-9, _ is applicable in String, and there is no space in both the string. Hence, if we print(c is d), the result is True.
String Method
Python has a set of built in function, that can be used with string.
- capitalize() - It convert the first character of string to uppercase and rest of the remaining character to lowercase.It does not modify the original string.
str1 = "welcome to fresherbell PYTHON tutorial"
print(str1.capitalize())
print(str1)
Output-
Welcome to fresherbell python tutorial
welcome to fresherbell PYTHON tutorial
In the above program "welcome" is converted to "Welcome" and "PYTHON" is converted to "python". But, it does not modify the original string.
- upper() - It convert all the lowercase character in string to uppercase.It does not modify the original string.
str1 = "welcome to fresherbell PYTHON tutorial"
print(str1.upper())
print(str1)
Output-
WELCOME TO FRESHERBELL PYTHON TUTORIAL
welcome to fresherbell PYTHON tutorial
In the above program, all lower case character in string str1 is converted into uppercase. But, it does not modify the original string.
- lower() - It convert all the uppercase character in string to lowercase.It does not modify the original string.
str1 = "WELcome To fresherBELL PYTHON TUTOrial"
print(str1.lower())
print(str1)
Output-
welcome to fresherbell python tutorial
WELcome To fresherBELL PYTHON TUTOrial
In the above program, all upper case character in string str1 is converted into lower case. But, it does not modify the original string.
- swapcase() - It swap (convert) all the uppercase character to lowercase and lowercase character to uppercase in the given string.It does not modify the original string.
str1 = "WELcome To fresherBELL PYTHON TUTOrial"
print(str1.swapcase())
print(str1)
Output-
welCOME tO FRESHERbell python tutoRIAL
WELcome To fresherBELL PYTHON TUTOrial
In the above program, it swap all the upper case character to lower case and all the lower case character to upper case of the string str1. But, it does not modify the original string.
- casefold() - This method is similar to lower(), but it converts more character to lowercase or case fold string aggressively than the actual lower() method. It does not modify the original string. For example, in German lowercase ß is equivalent to ss.
str1 = "Preß - Media"
print(str1.casefold())
print(str1)
Output-
press - media
Preß - Media
In the above program, ß is german lower case, using the case fold method it is aggressively changed to lowercase i.e ss.
- count() - It will return the number of occurrences of a substring in the given string. It is case sensitive
Syntax-
String.count(value, start, end)
#String- Value from where the substring needs to be searched
#value - It is a substring that needs to be searched in the given String.
#start - (Optional) It is a position to start the search, by default is 0
#end - (Optional) It is a position to end search,
by default is the end of the string
Example-
str1 = "Hello Python, Hello Fresherbell, Hello"
print(str1.count("Hello"))
print(str1.count('Hello',6,26))
print(str1.count("hello"))
Output-
3
1
0
In the above program,
Line 2- "Hello" is searched in whole string str1, and return count 3
Line 3- "Hello" is searched in string str1 index from 6 to 26, and return count 1
Line 4- "hello" is searched in whole string str1, and return count 0. because count function will perform case-sensitive search in whole string.
- center() - It will return the new string with center-aligned or equally padded on both ends using a specified character( or space by default).
Syntax-
String.center(length, fillchar)
#length - It is the total length of the string that needs to be returned
after filling specified character on both the end of actual string.
#fillchar - (Optional) It is specified character that need to be filled
on both the end of actual string. By default it is space
Example-
str1 = "Hello Python"
print(str1.center(30))
print(str1.center(30,'*'))
print(str1.center(2))
Output-
Hello Python
*********Hello Python*********
Hello Python
In the above program,
Line 2- str1 is equally centered from both the end using space as default specified character, with total length of new string as 30
Line 3- str1 is equally centered from both the end using * as default specified character, with total length of new string as 30
Line 4- On providing length less than the original string. then it will return the original string.
- endswith() - It will return boolean value, i.e True if string ends with specified value, otherwise it will return False.
Syntax-
String.endswith(value, start, end)
#value - It is a suffix(end value) that
needs to be checked in the given String.
#start - (Optional) It is a position to start
the search, by default,it is 0
#end - (Optional) It is a position to end search,
by default is the end of the string
Example-
str1 = "Hello Python"
print(str1.endswith("on"))
print(str1.endswith("Py",0,8))
print(str1.endswith("Hell"))
Output-
True
True
False
- split() - It will break or Split the string at the specified separator and return a list of string.
Syntax-
String.split(separator,maxsplit)
#separator- (Optional) Separator value at which
split should occur in the given string
#maxsplit- (Optional) Maximum number of splits that should occur in String.
By default there is no limit in number of split.
Example-
str1 = "Python is a cross-platform object-oriented general-purpose
, high-level programming language"
print(str1.split())
print(str1.split("-"))
print(str1.split("-",2))
Output-
['Python', 'is', 'a', 'cross-platform', 'object-oriented', 'general-purpose,',
'high-level', 'programming', 'language']
['Python is a cross', 'platform object', 'oriented general', 'purpose, high',
'level programming language']
['Python is a cross', 'platform object', 'oriented general-purpose,
high-level programming language']
- rsplit() - It will break or split the string from the right at the specified separator and return a list of strings. If maxsplit is not specified as a parameter, it will return the list same as split()
Syntax-
String.rsplit(separator,maxsplit)
#separator- (Optional) Separator value at which split
should occur in the given string
#maxsplit- (Optional) Maximum number of splits that
should occur in String Starting from right.
By default there is no limit on the number of splits.
Example-
str1 = "Python is a cross-platform object-oriented general-purpose,
high-level programming language"
print(str1.rsplit("-"))
print(str1.split("-"))
print(str1.rsplit("-",2))
print(str1.split("-",2))
Output-
['Python is a cross', 'platform object', 'oriented general', 'purpose, high',
'level programming language']
['Python is a cross', 'platform object', 'oriented general', 'purpose, high',
'level programming language']
['Python is a cross-platform object-oriented general', 'purpose, high',
'level programming language']
['Python is a cross', 'platform object', 'oriented general-purpose,
high-level programming language']
- title() - It will return a string, where the first character of each word is uppercase and the rest characters will be in lowercase. which is similar to the title.
Example-
str1 = "PythON is a CRoss-platFORM obJECT-oriENTED PROgramming lanGUAGE"
print(str1.title())
Output-
Python Is A Cross-Platform Object-Oriented Programming Language
- strip() - It will remove any character from starting(left end) and ending(right end) part of the string. by default, it will remove space. To remove other character we need to define it as a parameter of strip() method.
String.strip(character)
#character - A set of multiple character that need to
be removed from beginning and ending of a String
Example-
str1 = " Hello Welcome to python Tutorial "
print(str1.strip())
str2 = ",,,].rrHello Pythonggg,,,;;;"
print(str2.strip(",.rg;]"))
Output-
Hello Welcome to python Tutorial
Hello Python
- lstrip() - It will remove any character from starting( left end) part of the string. by default, it will remove space. To remove other character we need to define it as a parameter of lstrip() method.
String.lstrip(character)
#character - (Optional) A set of multiple character that need to
be removed from beginning of a String. by default it will take space
Example-
str1 = " Hello Welcome to python Tutorial "
print(str1.lstrip())
str2 = ",,,].rrHello Pythonggg,,,;;;"
print(str2.lstrip(",.rg;]"))
Output-
Hello Welcome to python Tutorial
Hello Pythonggg,,,;;;
- rstrip() - It will remove any character from ending( right end) part of the string. by default, it will remove space. To remove other character we need to define it as a parameter of lstrip() method.
Syntax-
String.lstrip(character)
#character - (Optional) A set of multiple character that need to
be removed from ending of a String.by default it will take space
Example-
str1 = " Hello Welcome to python Tutorial "
print(str1.rstrip())
str2 = ",,,].rrHello Pythonggg,,,;;;"
print(str2.rstrip(",.rg;]"))
Output-
Hello Welcome to python Tutorial
,,,].rrHello Python
- find() - It will return the index of first occurrence of specified value in the given string. If not found it will return -1. It is similar to index(), only difference is index() method will raise an exception if specified value is not found.
Syntax-
String.find(value, start, end)
#value - It is a specified value that
needs to be searched in the given String.
#start - (Optional) It is a position to start
the search, by default, it is 0
#end - (Optional) It is a position to end search,
by default is the end of the string
Example-
str1 = "Hello Welcome to python Tutorial"
print(str1.find("python"))
print(str1.find("Python"))#case sensitive
print(str1.find("to",10,30))
print(str1.find("to",20,30))
Output-
17
-1
14
26
- index() - It will return the index of first occurrence of specified value in the given string. If not found it will raise an exception. It is similar to find(), only difference is find() method will return -1 if specified value is not found.
Syntax-
String.index(value, start, end)
#value - It is a specified value that
needs to be searched in the given String.
#start - (Optional) It is a position to start
the search, by default, it is 0
#end - (Optional) It is a position to end search,
by default is the end of the string
Example-
str1 = "Hello Welcome to python Tutorial"
print(str1.index("python"))
print(str1.index("to",10,30))
print(str1.index("to",10,13))
print(str1.index("Python"))#case sensitive
Output-
17
14
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_6136/1803055335.py in <module>
2 print(str1.index("python"))
3 print(str1.index("to",10,30))
----> 4 print(str1.index("to",10,13))
5 print(str1.index("Python"))#case sensitive
ValueError: substring not found
- format() - It will formats the specified values and insert them inside a string placeholder. the placeholder will be defined in the form of curly bracket {}.
format() method will take any number of parameters. It is divided into two type of parameter i.e
Positional Parameter
Here the list of parameters can be accessed in an expression by using an index in placeholder {} or directly by sequential order. Hence , It is called as positional parameter format().
We can also modify the order of a variable in an expression by using indexing starting from 0 and can also change the format of the required value.
For example, c= 3847.0483773 is formatted in the form of 11.3f. where f represents format is dealing with float value. 3 define the number of digits after ".", i.e it will take 3847.048. 11 define the width the number 3847.048 can take. 3847.048 has current width of 8 including ".". remaining 3 (11-8) will be taken as padding(space) in front of number 3847.048 (i.e " 3847.048")
Example-
a="Python"
b="Fresherbell"
c= 3847.0483773
# By Default Order
print('Hello {}, Hello {}, Value of c is {}'.format(a,b,c))
# We can also change the order using indexing
print('Hello {1}, Hello {0}, Value of c is {2:11.3f}'.format(a,b,c))
Output-
Hello Python, Hello Fresherbell, Value of c is 3847.0483773
Hello Fresherbell, Hello Python, Value of c is 3847.048
Keyword Parameter
Here the list of parameters(i.e key-value pair) can be accessed in an expression by using a key in placeholder {}. And in the format() method we can define the key-value pair. Hence, It is called a keyword parameter format().
We can also change the format of the required value.
Example-
a="Python"
b="Fresherbell"
c= 3847.0483773
# By Default Order
print('Hello {name2}, Hello {name1}, Value of c is {value:11.3f}'.format(name1 = b,name2 = a,value = c))
Output-
Hello Python, Hello Fresherbell, Value of c is 3847.048