Reverse String In Python - codebugfree

Python reverse string

 In this section we learn how to do reverse the string in python. First you know the what is string. String is the one of the datatype of keyword. String is write the inverted comma. like "String". 

Reversing the string is the tricky point. It is easy if programmer logic is good builds. It is difficuilt in beginner programmer to reversing the string. We revese the string with 3 method.

The three method of reverse the string with the help of built in function, string slicing and last using loop concepts to reverse the string

To see the example of resturant oder the item and the item was reversing. 

print("Welcome Code bug free resturent")
print("food are been shorted as there alaories")
user_list = []#empty list append in for in range
user_update = int(input("Enter a size of list: "))
for i in range(user_update):
    user_list.append(input("Enter your list\n"))
print(f"your list is {user_list}")
print(f"Reversing user list by built in fuctiob {user_list}")
print(f"reversing list by strng slicing {user_list}")
reverse_me = user_list[:]
for i in range(len(user_list)//2):
    user_list[i],user_list[len(reverse_me)-i -1] = user_list[len(reverse_me)-i -1],user_list[i]
    print(f"see iteration i = {i} and reverseme {user_list}")

print(f"Reverse is my {reverse_me} ans {user_list}")

Her we first print the banner of resturent. create the empty list and get input the range for loop input the item user.

All item is append in that empty list. First using the reverse built in function reverse the string of an item. In second method use the string slicing trick to reverse the string.

At last method is quit diffcuilt we use for loop system to reverse the string in here range is user list /2. user list i is reverse. You see in out put. How to reverse the string use to for loop condition.

Welcome Code bug free resturent
food are been shorted as there alaories
Enter a size of list: 5
Enter your list
Enter your list
Enter your list
Enter your list
sell roti
Enter your list
your list is ['chowmin', 'momo', 'samosa', 'sell roti', 'yomari']
Reversing user list by built in fuctiob ['yomari', 'sell roti', 'samosa', 'momo', 'chowmin']
reversing list by strng slicing ['yomari', 'sell roti', 'samosa', 'momo', 'chowmin']
see iteration i = 0 and reverseme ['chowmin', 'sell roti', 'samosa', 'momo', 'yomari']
see iteration i = 1 and reverseme ['chowmin', 'momo', 'samosa', 'sell roti', 'yomari']
Reverse is my ['yomari', 'sell roti', 'samosa', 'momo', 'chowmin'] ans ['chowmin', 'momo', 'samosa', 'sell roti', 'yomari']

This much only for the example of reverse the string.

Post a Comment