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): → Defines a function named reverse_string that takes a string s as input.
Reverse the String:
return s[::-1] → Uses Python slicing to reverse the string.
s[start:stop:step] → Here, [::-1] means:
Start from the end (-1 step)
Move backward one character at a time
Continue until the beginning is reached
Calling the Function:
reverse_string("hello") → Returns "olleh".
reverse_string("Python") → Returns "nohtyP".
reverse_string("12345") → Returns "54321".
Example Output
olleh
nohtyP
54321
0 Comments:
Post a Comment