Project Level 1: Beginner
This project is designed for beginner learners who are still learning and practicing Python fundamentals.
Project Description
Your task for today is to write some code that reverses any string. There are two fundamentally different ways to accomplish this, one using a for-loop and another using string slicing. If you can, write both solutions.
How the Project Works
Start your script by defining a sentence as a string. Here is an example:
text = "Python is fun!"
Here is how the program behaves when run:
Prerequisites
Required Libraries:
Required Files: No files are required.
IDE: You can use any IDE on your computer to code the project.
Danger Zone
Once you code the project, compare it with our two solutions given in the button below.
inputstring = "Hello and welcome to donuts"
newstring = ''
for c in inputstring:
newstring = c + newstring
print(newstring)
My solution (I'm colombian):
"""
Autor: Juan Sebastian Valencia Londoño
Your task for today is to write some code that reverses any string.
There are two fundamentally different ways to accomplish this,
one using a for-loop and another using string slicing.
If you can, write both solutions.
"""
def reversed_string_1(string):
"""
Se usa el manejo de strings para retornar el string
:param string: str
:return: str
"""
reversed_string = string[::-1]
return f'The reversed string is: {reversed_string} - with algorithm 1'
def reversed_string_2(string):
reversed_string = []
for i in range(len(string)-1, -1, -1):
reversed_string.append(string[i])
reversed_string = ''.join(reversed_string)
return f'The reversed string is: {reversed_string} - with algorithm 1'
print(reversed_string_1('Hola retorname en reversa'))
print(reversed_string_2('Hola retorname en reversa'))