String in Python programming | Hands on Coding

  • String is a collection of characters.
  • It is created using single quotes or double quotes or triple quotes.
#string with single quotes
str1='I love PyTHON'
#string with double quotes
str2="I love PyTHON"
#string with triple quotes
str3='''I love PyTHON'''
  • We can print string using print() function.
  • We can also print particular character of string using index number.
  • String are stored in the form of array.

 

#creating string
str="PyTHON"
#printing string
print("String:",str)
#printing first character
print("First character:",str[0])
#printing second character
print("Second character:",str[1])
#printing last character
print("Last character:",str[-1])
"""
***Output***
String: PyTHON
First character: P
Second character: y
Last character: N
"""
 
  • We can access range of characters from string using slicing operator colon(:).

#creating string
str="I love PyTHON"
#printing string
print("String:",str)
#printing indexing 2 to 6 character
print("str[2:6]:",str[2:6])
#printing character between
# 7th and 2nd last character
print("str[7:-1]:",str[7:-1])
"""
***Output***
String: I love PyTHON
str[2:6]: love
str[7:-1]: PyTHO
"""
 
  • We can update string value by reassigning new value to the same variable.
  • But we can,t update the particular character of the string.

#creating string
str="Python Programming"
print("Original String:",str)
#update string str
str="Java Programming"
print("Updated String:",str)
"""
***Output***
Original String: Python Programming
Updated String: Java Programming
""" 
  • But we can,t update the particular character of the string.

#creating string
str="Python Programming"
print("Original String:",str)
#updating character P with M
#this line will raise an error
str[0]="M"
print("Updated String:",str)
"""
***Output***
TypeError: 'str' object does not support item assignment
"""
  • We can create multiline string using three double quotes.

#creating string
str="""Hello friends.
you can learn python easily.
"""
#printing string
print("Multiline String:",str)
"""
***Output***
Multiline String: Hello friends.
you can learn python easily.
"""
 
  • We can also create multiline string using three single quotes.

#creating string
str='''Hello friends.
you can learn python easily.
'''
#printing string
print("Multiline String:",str)
"""
***Output***
Multiline String: Hello friends.
you can learn python easily.
"""
  • in keyword is used to check specified character or group of characters is present or not.
  • in keyword returns true if specified character is present otherwise returns false.

#creating string
str="I love python programming"
search_str=input("Enter any string to search:")
if  search_str in str:
    print(search_str," is present")
else:
    print(search_str,"is not present")
"""
***Output***
Enter any string to search:python
python  is present
"""
 
  • Check specified string is not present in the string.

 #creating string
str="I love python programming"
print("java" not in str)
print("python" not in str)
"""
***Output***
True
False
"""
 
  • We can combine two or more than two string using plus (+) operator.

#creating string
str1="I love "
str2="python programming"
str3=str1+str2
print("String1:",str1)
print("String2:",str2)
print("String3:",str3)
"""
***Output***
String1: I love 
String2: python programming
String3: I love python programming
"""
 
  • We can't combine string with numeric value.

#creating string
book="xyz"
price=550
#this line will raise an error
str3="The price of book "+book+" is Rs."+price
print("Combined String:",str3)
"""
***Output***
 str3="The price of book "+book+" is Rs."+price
TypeError: can only concatenate str (not "int") to str
"""

  • To combine string with numeric value format() function is used.

#creating string
book="xyz"
price=550
#combining string with numeric value
str3="The price of book {} is Rs. {}".format(book,price)
print("Combined String:",str3)
"""
***Output***
String: The price of book xyz is Rs. 550
"""
  • asterisk (*) symbol is used to concate multiple copies of the same string.

str="Python ";
#print Python 5 times
print(str*5)
"""
***Output***
Python Python Python Python Python 
"""