Complete Guide to Python String
Initialization
Access
- The in-operator can be used to check a substring exists in a string.
- The subscript-operator can be used for accessing a character using the correspoding index. IndexError-exception will be thrown for non-existing indices.
- The slice-operator [start:], [start:end], [:end]
- Negative integer can be used to start from the last. [-start:], [-start:-end], [:-end]
- Iteration through characters.
Update
- You cannot modify a string since it is immutable.
Methods
- str1.format().
- str1.split(delimeter_str).
- str1.replace(search_str, replace_str).
- It won’t modify the existing string but return the modified string.
Operators
- Concatenation operator: +
- Multiplication operator: *
Interview Questions
1. When you access a character using subscript operator, Do you get an element of type character or string?
There is no character type in python. It is a string of length 1.