For all the self-taught geeks out there, here is our content library with most of the learning materials we have produced throughout the years.
It makes sense to start learning by reading and watching videos about fundamentals and how things work.
Data Science and Machine Learning - 16 wks
Full-Stack Software Developer - 16w
Search from all Lessons
Curated list of small interactive and incremental exercises you can take to get better at any coding skill.
Curated section of projects to build while learning with simple instructions, videos, solutions, and more.
Guides on different topics related to the technologies that we teach in our courses
Social & live learning
The most efficient way to learn: Join a cohort with classmates just like you, live streams, impromptu coding sessions, live tutorials with real experts, and stay motivated.
Javier Leyva Seiglie
The fastest and simplest way to achieve the desired result would be to use an extended slice:
Taking in consideration that Python library doesn´t support the built-in method
reverse() as we would do, to name just one, the list container, we are left to ask: How to reverse a string in Python? Well, here are a few ways to do it, so let´s dig in:
Using steps to reverse a string is fast and simple:
Taking advantage of the extended slice fields
[start:stop:step], we can reverse a string while we DO NOT stablish either start and stop fields. If we do not pass those values, default for
start field will be 0 and default for the
end field will be the length of the string. Passing -1 to the
step field indicates that we want to start taking our steps from the end of the element and finish in the start.
If you use the reversed() method by itself, it will throw the reference on the memory instead of the reversed string, like this:
You may ask why? Because it returns the reversed iterator of the string. We use join() to, as the name suggest, join them and achieve the desired result
Yes, we can use our old and trusty companion to reverse a string. You must be already familiarize with loops, so here is the code example to make it happen:
We iterate through the given string (myString) and store its characters on the string variable. On every loop, we will replace the string value with the new character taken from myString store it and add to the end of the string variable the value that was on the string variable on the previous loop.
If you are a Die-Hard fan of lists, want to use the
reverse() method and don´t like the other options, here´s how to do it:
We turned the string into a list, then we are able to use the reverse() method on the list we just created and then we turn it back joining the list element into a string.
We covered different approaches regarding how to reverse a string in Python since the
reverse() method is not supported on Strings. We discussed the steps approach,
reversed() method, loops and using lists with the
reverse() method and how each and every one works behind the scene. Choosing which one to use depends on how much comfortable you feel with each of the options and the needs of the project.
Hope you enjoy the reading and keep on the Geek side!