How To Get First And Last N Characters From A String In Python
It can be quite easy to get the first character in a string, but how can you get the first n characters from a string? Or what about the last n characters? To get more than one character from a string you can use the slice operator which has the syntax [start:stop:step]. For example, to get the first 5 characters from a string variable my_string use the slice operator as follows my_string[:5] or to get the last 5 characters from a string use my_string[-5:]. ...