A string slice is a part of a string containing some contiguous characters from the string, where the strings are sliced using a range of indexes.
For a string s
s[start:end] where start and end are integers and legal indices, Python returns a slice of the string by returning elements beginning at index start and extending upto but not including end
Character at index end won’t be included in the string slice If start is missing, start will be taken as 0(the first index) If end is missing, end will be taken as length of the string
Example Codes :
word= “amazing”
For any index n ( positive, negative or out of bounds) s[:n]+s[n:] will result in the original string s
s[start:end:skip] skip here is an optional third index. If skip is specified, the string slice would include indexes start, start+skip and so on.
Example code: Write a program to input two strings if string1 is contained in string2, then create a third string with the first four characters of string2, added with the word “Restore”.
Example code: Write a program to input a string and check if it is a palindrome string using a string slice.