1. Using Python Slicing
Program:
1
2
3
4
5
6
7
8
| string1=input("Enter the String:")
#using slicing
#slicing is the fastest way to reverse string in python
l=string1[::-1]
print("\n\t>> Reverse String:",l)
|
Expected O/P:
Enter the String:ABCD
>> Reverse String: DCBA
NOTE:- Slicing is the Fasted and the Easiest way to reverse a string in the Python.
2. Using For Loop
Program:
1
2
3
4
5
6
| string1=input("Enter the String:")
l=''
for i in string1:
l= i + l
print("\n\t>> Reverse String:",l)
|
Expected O/P:
Enter the String:COMPUTER
>> Reverse String: RETUPMOC
3. Using Python List
Program:
1
2
3
4
5
6
7
8
9
10
11
| string1=input("Enter the String:")
#converting string to list
string=list(string1)
string.reverse()
#converting list to string
l="".join(string)
print("\n\t>> Reverse String:",l)
|
Expected O/P:
1
2
3
| Enter the String:PYTHON
>> Reverse String: NOHTYP
|
1 Comments
Wow! Thank you! I constantly wanted to write on my site something like that. Can I take a portion of your post to my website?. Best python string methods service provider.
ReplyDeletePost a Comment