store one string to another string in python by R4R Team

we have store the one string into another string in reverse order like an example -
s="Mytext"
then it reverse in other string is
s1="txetym"

program -

print("Enter the string")
s=input()
print("Input string is -"+s)

#first way
s1=""
l=len(s)
for i in range(l):
s1=s1+s[l-i-1]
print("string after reverse by first method is - "+s1)

#second way
s1=s[::-1]
print("string after reverse by second method is -"+s1)


output -

Enter the string
mytext
Input string is -mytext
String after reverse by first method is - txetym
String after reverse by second method is - txetym


- In this program, first method use the looping concept and we traverse the given string from the backside and store in the new string so new string get the reverse of the given string, this type of concept are also possible in some other programming language like C,C++,JAVA.

- but in second method we use something like s[::-1],Generally in s[x:y:z], x and y define the starting and ending range of the string and z define the order of traversing, so in this program we use s[::-1] so here x is 0 and y is actual length and z is -1.
- Why -1 ? , because here we want to traverse our given string in reverse order so we take -1.
- If we want to copy given string in another string directly so we use simply s[::1].
Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!