Python – Strings Operations with Example’s

In Python Strings can be created with single or double quote it does’nt matters . where as “hello”+”world” will be print as helloworld  (no gap)   and “hello”,”worlds will be print as hello world (with gap)

if you add operator between stings it will add the both strings which is called  String Concatenation

Example 1 

print(“good” + “morning”)
output : goodmorning

If your strings contains no.  integer values it will be added  using (+) sign will add numbers

Example 2

print( 5 +6)

output :  11

print (“5″+”6”)

output : 56

Note : You cant add string type to the numeric type

Example :

print (5 +”6″)

output : Traceback (most recent call last): File “..\Playground\”, line 1, in <module> \ufeffprint( 5 +”6″) TypeError: unsupported operand type(s) for +: ‘int’ and ‘str’

In Python Strings can also be multiplied.

Srings can be multiplied with integer but not with strings or float

 

Example 3

print(“hello” * 5)

output : hellohellohellohellohello

print (hello * hello)

output : Traceback (most recent call last): File “..\Playground\”, line 1, in <module> \ufeffprint(hello * hello)

 

Hits: 91

Share your thoughts