strA = "Hello "
strB = 'World!'
#Printing the above variables on screen
print strA #This will work in Python2
print strB#This will work in Python2
print( strA ) #This will work in Python3
print( strB )#This will work in Python3
|Result of above Python3 Code|
print( strA ) #prints the first character of variable strA
print( strA[1:3] ) #prints characters from first index to third
print( strA[3:] ) #prints characters from third index
print( "Print Concatenated Output: " + strA + strB )
print( strA + 4 ) #This will give an error as mentioned above
#Correct Way to Concat String and another Datatype
print( strA, 4 ) #Method 1
print( strA + str(1234) ) #Method 2
Some of the special operators
"""You can have multiple formatting operators, but remember the sequence of variables must be followed after % inside a bracket () separated by comma"""
num = 2
post_num = 129
print( "Code %s Learn" %num)
print( "Code %s Learn\'s post number: %s" %(num,post_num))
|String Formatting Example Output|
Check Python Docs for detailed reference.