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
|
%x |
Result
Hi Homer |