Python: Reverse a string
Python String: Exercise-39 with Solution
Write a Python program to reverse a string.
Sample Solution:
Python Code:
# Define a function 'reverse_string' that takes a string 'str1' as input.
# The function returns the reversed version of the input string.
def reverse_string(str1):
return ''.join(reversed(str1))
# Print an empty line for spacing.
print()
# Call the 'reverse_string' function with the input "abcdef", reverse the string, and print the result.
print(reverse_string("abcdef"))
# Call the 'reverse_string' function with the input "Python Exercises.", reverse the string, and print the result.
print(reverse_string("Python Exercises."))
# Print an empty line for spacing.
print()
Sample Output:
fedcba .sesicrexE nohtyP
Flowchart:
Python Code Editor:
Previous: Write a Python program to count occurrences of a substring in a string.
Next: Write a Python program to reverse words in a string.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/string/python-data-type-string-exercise-39.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics