Python strings

Strings are the most famous data types in any programming language. Python strings belong to an in built class named str. Python considers anything that is in single quotes or double quotes as a string element. So, ‘Hi !’ as well as “Hi !” are both considered as Strings.

To get started with Strings, let’s try a small exercise. Look at the following example, It shows how for loop can be used to retrieve every letter from the string ‘welcome’.

var1 = 'welcome'
n = 1
for item in var1:
    print ('letter no. ' + str(n) + ' is : ' + item)
    n = n + 1;

Download the code Run the code
Have a look at the for loop code:

var1 = 'welcome'
for item in var1:
    print ('letter no. ' + str(n) + ' is : ' + item)
    n = n + 1;

1. for item in var1:  In this statement, item refers to alphabet in the string var1. So, in this for loop, every letter will be assigned to the variable ‘item’ one by one. Once a letter is assigned all the steps mentioned in the for loop are executed and then next letter will be assigned to the variable ‘item’. You will learn more about for loops in chapter on loops.
2. print(‘letter no. ‘+str(n)+’ is : ‘+item) Observe this statement properly. Variable n has the value of 1. Hence, it is an integer. Print command requires that the value in the parentheses should be a string. Hence, to convert value of integer n to string value before printing we use the statement str(n).
3. n = n+1;  n is incremented. We want the count of n to increase by one every time a new alphabet is assigned to item.

Can you guess the outcome of the code given below:

var1 = 'welcome'
n = 0
while n < 7:
    print ('letter no. ' + str(n+1) + ' is : ' + var1[n])
    n = n + 1;

Download the code Run the code

Well, the output will be the same as the previous example. The only difference is that we have used the while loop instead of for loop to get the results. You will read about ‘while’ loop in detail when you reach the chapter on Loops.

Printing a substring

To print a substring you can do the following: suppose you just want to print first three letters of a string, in that case you will have to slice the string along the index. Every character in a string has an index. The first character has an index of 0 the second has an index of 1 and so on.

var1 = 'welcome'
print (var1[0:3])

Download the code Run the code

String updating

Strings can be updated if we reassign a new value to the variable. Changes can also be made to the existing string.

var1  = "welcome!!"
print ('The string is : ' + var1)
var1 = var1[:7] + " John!!"
print ('The updated value of the string is : ' + var1)

Download the code Run the code

Strings in Triple Quotes

If you want a variable to hold multiple lines of string then you can use triple quotes “””

var1  = """Hi
Hello
How are you?
I am Fine"""

print (var1)

Download the code Run the code

Quotes within Quotes

If a string variable is defined in double quotes then it can have single quotes in it without any issues. Similarly, if a string is defined within single quotes then it can have double quotes in it without any issues.

var1 = "'Hi'"
var2 = '"Hi"'
print (var1)
print (var2)

Download the code Run the code

<<< Python membership and identity operatorsPython escape characters >>>
Copyright 2005-2016 KnowledgeHills. Privacy Policy. Contact .