remove last two digits on string in python

You can remove the last two digits of a string in python using string slicing. Here is an example code block:

main.py
my_string = "1234567890"
new_string = my_string[:-2]
print(new_string)
71 chars
4 lines

Output:

main.py
12345678
9 chars
2 lines

In the example above, the [:-2] notation indicates that we're slicing the string from the first character to the second last character. This effectively removes the last two digits from the string. You can replace my_string with the string variable you want to modify.

related categories

gistlibby LogSnag