Reverse a string.
If the input is: Hello World.
The output should be: .dlroW olleH
A string is almost like a list. A list of characters. You can access any element by index. You can run a for loop on it. You will get each character as an element. Besides, you can also add two string or join two characters
my_string = "You are my Hero"
print('Access by index')
print('-------------')
print(my_string[0])
print(my_string[1])
print(my_string[4])
print('Run a for loop')
print('-------------')
for char in my_string:
print(char)
print('now join strings')
print('-------------')
print('hello' + 'world')
print('h'+'w')
However, a string is not a list.
def reverse_string(str):
reverse = ""
for char in str:
reverse = char + reverse
return reverse
str = input("Enter your string: ")
result = reverse_string(str)
print(result)
A simple trick will make it work.
Let’s say you wrote “Hello world.” if you break down it or run a for loop and get each character at a time. You will get first H and then e and then l … and so on.
Now, while adding each character add it before the previous one.
Hence, first you will have H. then while adding e, you will add it before the H. It will become eH.
Then “l” will come and add it before. This will give you-
leH
Keep doing it. And you will get
.dlroW olleH
Now if you look at the code you will understand why we did-
reverse = char + reverse
What does it mean by the word immutable?
- Immortal
- Not changeable
- Changeable
Show Answer
The answer is: 2
Will you be able to append, remove or set a character to string by the index?
- Yes
- No
- Sometimes
Show Answer
The answer is: 2
A string is an immutable ordered sequence.
tags: programming-hero
python
python3
problem-solving
programming
coding-challenge
interview
learn-python
python-tutorial
programming-exercises
programming-challenges
programming-fundamentals
programming-contest
python-coding-challenges
python-problem-solving