Give at least three examples that show different features of string slices. Describe the feature illustrated by each example. Invent your own examples. Do not copy them for the textbook or any other source.
# 1
# If you need to reverse the string
def reverse_str(str):
return str[::-1]
print('OUTPUT reverse_str:', reverse_str('GlobalFreelance'))
# 2
# If you need to take the last 4 characters in a string
def year_in_str(str):
return str[-4:]
print('OUTPUT year_in_str:', year_in_str('13-05-2022'))
# 3
# If you need to take every second char in string
def every_2_char(str):
return str[1::2]
print('OUTPUT every_2_char:', every_2_char('BE BRAVE LIKE UKRAINE'))
OUTPUT reverse_str: ecnaleerFlabolG
OUTPUT year_in_str: 2022
OUTPUT every_2_char: EBAELK KAN
Comments
Leave a comment