def reverse_string(s):
return s[::-1]
print(reverse_string("hello"))
print(reverse_string("Python"))
print(reverse_string("12345"))
#source code --> clcoding.com
Code Explanation:
Define the function
def reverse_string(s): → Creates a function reverse_string that takes a string s as input.
Reverse the string using slicing
s[::-1] → Uses negative slicing to reverse the string.
s[start:stop:step]
s[::-1] → Starts from the end (-1 step) and moves backward.
Return the reversed string
return s[::-1] → Returns the reversed version of the input string.
Call the function and print results
reverse_string("hello") → Returns "olleh".
reverse_string("Python") → Returns "nohtyP".
Example Output
olleh
nohtyP
54321
0 Comments:
Post a Comment