Introduction to String


☞ It is a sequence of characters enclosed within single quotes('…') or double quotes("…") or triple quotes('''…''' or """…""")

Syntax
 
variable_name = 'value'
        OR
variable_name = "value"
        OR
variable_name = '''value'''
        OR
Variable_name = """value"""
    
Example :
 
s1='inspire'       #single quotes
print(s1)

s2="web"         #double quotes
print(s2)

s3='''soft'''         #triple single quotes
print(s3)

s4=""".com"""    #triple double quotes
print(s4)          
    
Output :
 
inspire
web
soft
.com        
    

Elements are addressed using their index value :

  • The indices of a string begin from 0 to (length-1) in forward direction and -1,-2,-3….,-length in backward direction.
Example :
s = "PYTHON"
Forward Indexing 0 1 2 3 4 5
String s P Y T H O N
Backward Indexing -6 -5 -4 -3 -2 -1

Creating an empty string :

Example :
 
S=''
print(S)   
    
Output :
 
#will not print anything   
    

It is immutable i.e. you cannot change it :

Example :
 
S="inspire"
S[5]='K'
print(S)   
    
Output :
 
S[5]='K'
TypeError: 'str' object does not support item assignment