Python 3 String Operators

List of string operators available in Python 3.

Operator Description Operation Example
+ Concatenates (joins) string1 and string2 string1 + string2
Result
Tea Leaf
* Repeats the string for as many times as specified by x string * x
Result
Bee Bee Bee
[] Slice — Returns the character from the index provided at x. string[x]
Result
e
[:] Range Slice — Returns the characters from the range provided at x:y. string[x:y]
Result
room
in Membership — Returns True if x exists in the string. Can be multiple characters. x in string
Result
True
False
True
not in Membership — Returns True if x does not exist in the string. Can be multiple characters. x not in string
Result
False
True
False
r Suppresses an escape sequence (\x) so that it is actually rendered. In other words, it prevents the escape character from being an escape character. r"\x"
Result
1     Bee
2\tTea
%

Performs string formatting. It can be used as a placeholder for another value to be inserted into the string. The % symbol is a prefix to another character (x) which defines the type of value to be inserted. The value to be inserted is listed at the end of the string after another % character.

Character Description
%c Character.
%s String conversion via str() prior to formatting.
%i Signed decimal integer.
%d Signed decimal integer.
%u Unsigned decimal integer.
%o Octal integer.
%x Hexadecimal integer using lowercase letters.
%X Hexadecimal integer using uppercase letters.
%e Exponential notation with lowercase e.
%E Exponential notation with uppercase e.
%f Floating point real number.
%g The shorter of %f and %e.
%G The shorter of %f and %E.
%x
Result
Hi Homer