The reason for writing this article is that I often use slices for the first time in competition pros, and my mind is getting crowded, so I decided to summarize it easily. It's just a slice note.
A string of numbers. The reason I chose a number string is because I thought the output result would be easy to understand.
s = "12345"
print(s[-1]) #end
5 #Output result
print(s[:-1]) #Delete the end
1234
print(s[0]) #lead
1
print(s[0:]) #From beginning to end
12345
print(s[1:]) #From first to last
2345
start = "6" + s[1:]
print(start) #Remove the beginning and add 6 to the beginning
62345
end = s[:-1] + "6"
print(end) #Remove the end and add 6 to the end
12346
Recommended Posts