Quite often I have contact with the objects through their names, and accordingly I perform various operations with a text variable. Now I show several important things about strings in Python.
The most interesting moments in my opinion. If you have some supplements, please write in comments:
1) the length of the line. It’s quite simple – len (s)
2) You can glue the two lines through the operator + It is also quite simple – s = ‘prefix’ + s
3) an interesting point, if you want to remove the first <n> characters of the string
s = s [n:]
In this case the square brackets is given the range in which the characters will return a string. [start: end]. If you miss <start> index, the line will begin with the first character, if you miss the <end> index, the line will end the last symbol.
4) By analogy with the previous example, if you want to remove from a string of recent <n> characters
s = s [:-n]
If the index is positive, the numbering of characters starts from the begining. Otherwise, if there was a minus sing, the characters numbering starts from the end of the string.
5) to replace the part of the string
s = s.replace ( ‘camera’, ‘object’)
in this case, the line will look into substrings ‘camera’, and if successful – replaced by the ‘object’.
That’s it, good luck!