Strings are one of the most popular data types in Python. Strings are created by enclosing various characters within quotes. Python does not distinguish between single quotes and double quotes.
Characters are building blocks of python. A program is composed of a sequence of characters. When a sequence of characters is grouped togther, a meaniningful string is created. Thus, a string is a sequence of charactersntreated as a single unit.
In many languages, strings are treated as arrays of characters but in Python, a string is an object of the str class. This string class has many constructors.
1. The str Class
Strings are the objects of str class. We can create a string using the constructor of str class as
s1=str() # Creates an empty string object
s2=str("Hello Vihaan") #Creates a string object for Hello Vihaan
An alternative way to create a string object is by assigning a string value to a variable.
s1="" # Creates empty string
s2="Hello Vihaan" #Equivalent to s2=str("Hello Vihaan")
2. Compound Data Types
So far we have seen five types: int, float, bool, NoneType and str. Strings are qualitatively different from the other four because they are made up of smaller pieces — characters.
Types that comprise smaller pieces are called compound data types. Depending on what we are doing, we may want to treat a compound data type as a single thing, or we may want to access its parts. This ambiguity is useful.
In order to access a part of the string, a square bracket operator [ ] must be used.
s="Vihaan"
print(s[2])
#Output
h
In the above example, Vihaan is stored in a variable s. Then the element of the s variable with index 4 is printed. As we know, in programming,every index starts with 0. Hence index 2 means the 3rd letter of the string. The 3rd letter of the string Vihaan is h. Hence, h is displayed in output.
s="Vihaan"
print(s[0])
#Output
V
3. len( ) Function
len() is a buit-in function in Python. The len() function returns the number of characters in a string:
s="Vihaan"
print(len(s))
#Output
6
To get the last letter of a string, you might be tempted to try something like this:
s="Vihaan"
length=len(s)
last=s[length]
print(last)
#Output
IndexError: string index out of range
That won’t work. It causes the runtime error IndexError: string index out of range. The reason is that there is no 6th letter in "Vihaan". Since we started counting at zero, the six letters are numbered 0 to 5. To get the last character, we have to subtract 1 from length:
s="Vihaan"
length=len(s)
last=s[length-1]
print(last)
#Output
n
Alternatively, we can use negative indices, which count backward from the end of the string. The expression s[-1] yields the last letter, s[-2] yields the second to last, and so on.
4. String Traversing
String can be traversal using while or for loop.
4.1 Using while Loop
A lot of computations involve processing a string one character at a time. Often they start at the beginning, select each character in turn, do something to it, and continue until the end. This pattern of processing is called a traversal. One way to encode a traversal is with a while statement:
s="Vihaan"
i = 0
while i < len(s):
print(s[i], end="")
i=i+ 1
#Output
Vihaan
This loop traverses the string and displays each letter on a line by itself. The loop condition is i < len(s), so when i is equal to the length of the string, the condition is false, and the body of the loop is not executed. The last character accessed is the one with the index len(s)-1, which is the last character in the string.
4.2 Using for Loop
Using an index to traverse a set of values is so common that Python provides an alternative, simpler syntax — the for loop:
s="Vihaan"
for i in s:
print(i,end="")
#Output
Vihaan
Alternatively
s="Vihaan"
for i in range(0,len(s)):
print(s[i],end="")
#Output
Vihaan
5. Strings are immutable
Strings are immutable which means that we can not change any element of the string. If we want to change any element of a string, we have to create a new string.
s="Hello Vihaan"
s[0]="P"
print(s)
#Output
TypeError: 'str' object does not support item assignment
Here, we try to change the 0th index of the string to a character P, but the python interpretergenerates an error.
Now,the solution to this problem is to generate a new string rather than change the old string
s1="Hello Vihaan"
s2="P"+s1[1:]
print(s1)
print(s2)
#Output
Hello Vihaan
Pello Vihaan
Now consider the following two similar strings. "Vihaan" is assigned to two different variables as:
s1="Vihaan"
s2="Vihaan"
In above example, both the variables, s1 and s2 have the same contents. Thus, Python uses one object for each string which has the same content as shown in following figure. s1 and s2 refers to the same string object, whereas s1 and s2 have the same ID number.
s1="Vihaan"
s2="Vihaan"
print(id(s1))
print(id(s2))
#Output
57828480
57828480