Home »
Python
Python Strings and its implementation
Learn: What is the String in Python, string’s declarations, define and its implementation in Python with Examples and Explanations?
Submitted by Shubham Singh Rajawat, on August 05, 2017
Strings in python are immutable means they cannot be changed once defined.
Example 1: #program to show that strings are immutable
string ="include"
string[5]="i"
Output
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
string[5]="i"
TypeError: 'str' object does not support item assignment
This proves that python for badges to change a pre defined string but we can do one thing we can reassign its value.
Example 2: #program to show that a string can be appended /reassigned
string="include"
print (string)
string+="help"
print (string)
Output
include
includehelp
So we append a string by reassigning its value.
Strings in python are most likely to be same as strings in other languages and are accessible in the same way also. We can define a string in two ways by using single quotes or double quotes.
Example 3: #program to define a string
string1="includehelp"
print (string1)
string2='includehelp'
print(string2)
Output
includehelp
includehelp
See outputs for both the strings are equal because they are considered same by python interpreter.
Indexing of strings in python starts with zero to length-1 i.e. if we want to access the first element of string we will use string[0]. Indexing also available from reverse side i.e. to access the last element we can also use string[-1] and string[-2] for second last element.
Example 4: #program to access a string
string="include help"
print(string[5])
print("include help"[5])
print(string[-1])
print(string[12])
Output
d
d
p
Traceback (most recent call last):
File "<pyshell#8>>, line 1, in <module>
print(string[12])
IndexError: string index out of range
strings can also be accessed like we accessed in line 3 of the above program but you should avoid using strings like this as every time you want to access any element of the string you have to write whole strings it would be much more difficult to do that, in line 4 error occurs because python checks for index out of bound condition.
We can also use triple quotes as a string but if a bare is used just after ‘def fun() ’ (any function) or at the starting of any module it will considered as ‘doc string ’ which contains the documentation of its object.
String slicing
String slicing means accessing a substring from the given string, this can be done by using a simple syntax.
string_name[beg:end:step]
Here,
beg : is the beginning index
end : is the end index but end is not inclusive
step : distance between every word (steps are optional)
If step is given -1 means a reverse string is returned.
Example 5: #program to implement string slicing
string="includehelp is a portal to learn concepts"
print(string[3:7])
print(string[:7])
print(string[15:])
print(string[0::3])
print(string[7:3:-1])
print(string[::-1])
Output
lude
include
a portal to learn concepts
ileliaoatlrcct
hedu
stpecnoc nrael ot latrop a si plehedulcni
TOP Interview Coding Problems/Challenges