Answer to Question #340531 in Python for Rogger

Question #340531

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
Expert's answer
2022-05-13T17:01:57-0400
# 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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS