Scripting >> Python >> Examples >> Operators >> String operations

 

Operation Result Examples
x in s True if an item of s is equal to x, else False  
x not in s False if an item of s is equal to x, else True  
s + t the concatenation of s and t >>> str = 'Hello World!'
>>> print (str + "TEST") # Prints concatenated string
Hello World!TEST
s * n, n * s equivalent to adding s to itself n times

>>> str = 'Hello World!'
>>> print (str * 2)      # Prints string two times
Hello World!Hello World!

s[i] ith item of s, origin 0 >>> str = 'Hello World!'
>>> print (str[0])     # Prints first character of the string
H
s[i:j] slice of s from i to j >>> print (str[2:5])     # Prints characters starting from 3rd to 5th
llo
s[i:j:k] slice of s from i to j with step k >>> str="Hello, World!"
>>> print(str[1:10:2])
el,Wr
len(s) length of s  
min(s) smallest item of s  
max(s) largest item of s  
s.index(x) index of the first occurrence of x in s  
s.count(x) total number of occurrences of x in s