208k views
0 votes
Write a function that accepts a string as an argument and then prints the string backwards.

Write in Python.



7

2 Answers

1 vote
Sure, here's a Python function that accepts a string as an argument and prints it backwards:

def print_backwards(s):

print( s[: :-1] )

Here, the [::-1] slice notation is used to reverse the string. When s is a string, s[::-1] will produce a new string with the characters in reverse order. The print() function is then used to output this reversed string.

To use this function, simply call it with a string as an argument:

print_backwards("hello world")

This will output:

dlrow olleh
User Orsiris De Jong
by
8.4k points
7 votes

Answer:

Here's a Python function that takes a string as input and prints the reversed version of the string:

def print_backwards(string):

reversed_string = string[::-1]

print(reversed_string)

The [::-1] syntax in string[::-1] returns the reversed version of the input string. The function then assigns the reversed string to a variable reversed_string and prints it using the built-in print() function.

Here's an example usage of the function:

>>> print_backwards("Hello, World!")

!dlroW ,olleH

>>> print_backwards("123456789")

987654321

User Pinser
by
8.0k points