String
String
Strings
A string datatype hold string data-any number of valid characters into a set
quotation marks. Each character in a string is a Unicode character. A string can
hold any type of known characters ie letters, numbers and special characters.
Python strings are character enclosed in quotation of any type-single quotation
mark, double quotation mark and triple quotation mark. Empty or null string is
a string that has 0 characters. Examples of valid string:-“abcd”, “1234”,
‘$%^&’.
Python strings are immutable. Strings are sequence of characters where each
character has a unique position called index. Index of a string begin from 0 to
length-1 in forward direction and -1, -2, -3….. –length in backward direction.
Strings
A Python string is a sequence of characters and each characters and each character
can be individually accessed using its index. Strings in Python are stored as individual
characters in contiguous location, with 2 way index for each location.
Let us consider the string say str1=“DPS Bopal”
It will be stored as
Forward Indexing 0 1 2 3 4 5 6 7 8
D P S B o p a l
Backward Indexing -9 -8 -7 -6 -5 -4 -3 -2 -1
Length of the string variable can be determined using len(<string>) function.
str1[1]=‘P’ str1[4]=‘B’ str1[7]=‘a’
str1[-4]=‘o’ str1[-9]=‘D’ str1[-2]=‘a’
The index is also called subscript and index is enclosed square brackets.
Strings
In a string, there is no index equal to the length of the string.
For example:
str1=“DPS Bopal”
print(str1[9]) # will show error.