Line slices

In Python, you can select part of a string (substring). To do this, use the operation of obtaining a slice (from the English slicing)
The general view of the slicing operation is as follows
s[start:stop:step]
This command takes a slice from the string s starting from the character at index start up to the character at index stop (not including it) with step step (if not specified, step is 1)
Any part in brackets may be missing.
For example,
s1 = s[3:8]
means that characters from 3 to 7 are copied into string s1 from string s with step 1.
You can take negative indices, then the count is from the end of the string.
s = "0123456789"
s1 = s[-7:-2] # s1="34567"
If start is not specified, it is considered to be equal to zero (that is, we take it from the beginning of the string). If stop is not specified, then the slice is taken until the end of the string.
s = "0123456789"
s1 = s[:4] # s1="0123"
s2 = s[-4:] # s2="6789"
This is how easy it is to reverse a string:
s = "0123456789"
s1 = s[::-1] # s1="9876543210"
All characters of the string are iterated in increments of -1, which means that the characters will be iterated from the end. The entire row is involved because the start and end indexes are not specified.

Delete and Paste

To delete part of a string, you need to compose a new string by combining the parts of the original string before and after the section to be deleted.
s = "0123456789"
s1 = s[:3]+s[9:] # s1="0129"
Using slices and string concatenation, you can insert a substring inside a string.
s = "0123456789"
s1 = s[:3]+"ABC"+s[3:] # s1="012ABC3456789"

String manipulation methods

Python has many methods (out-of-the-box functions) for working with strings.  Many of them are called using dot notation and are called methods. A complete list of string manipulation methods can be found online. 
Let's get acquainted with some of them. s = "aAbBcC" sUp = s.upper() # sUp = "AABBCC" - a method that translates   # uppercase each character of the string sLow = s.lower() # sLow = "aabbcc" - a method that translates   # lowercase each character of the string To the left of the dot is the name of the string (or the string itself in quotation marks) to which the method is to be applied, and to the right of the dot is the name of the method. The method is always written with parentheses. There can be any parameters inside the brackets if they are needed.

Previously, we already used the method of working with strings when we displayed data on the screen in a certain format - the format() method a = 4 b = 5 print("{}+{}={}".format(a,b,a+b)) # 4+5=9 Another useful method  isdigit() is a method to check if all characters of a string are digits, it returns a boolean value (True or False). s = "ab1c" print(s.isdigit()) #False s = "123" print(s.isdigit()) #True The useful strip() method allows you to remove spaces at the beginning and end of a string s = " ab 1c " print('s=', s.strip()) # s=ab 1c

Search in line

To search within a string in Python, use the find() method.
It has three forms and returns the index of the 1st occurrence of the substring in the string:
1)  find(str) - substring str is searched from the beginning of the string to its end;
2) find(str, start) - using the start parameter, the starting index is set, and it is from it that the search is performed;
3) find(str, start, end) - using the end parameter, the end index is set, the search is performed before it.

When the substring is not found, the method returns -1:

welcome = "Hello world! Goodbye world!" index = welcome.find("wor") print(index) #6 # look for from 10th to 15th index index = welcome.find("wor", 10, 15) print(index) # -1 You can search from the end of the string. For this, the rfind() method (from the English reverse find) is used - it returns the index of the last occurrence of a substring in a string.
 
Note: data methods do not look for the number of occurrences, but only determine whether there is such a substring in the string or not.

Replacing substrings in a string

In Python, to replace one substring with another in a string, use the replace() method: 
replace(old, new) - substring old is replaced by new;
replace(old, new, num) - parameter num shows how many occurrences of substring old replaced by new >.

 

Example
phone = "+1-234-567-89-10" # hyphens are changed to spaces edited_phone = phone.replace("-", " ") print(edited_phone) # +1 234 567 89 10 # hyphens are removed edited_phone = phone.replace("-", "") print(edited_phone) # +12345678910 # only the first hyphen changes edited_phone = phone.replace("-", "", 1) print(edited_phone) # +1234-567-89-10