Python Basics Exercises: Strings and String Methods (Summary)
In this video course, you’ve reinforced your knowledge of Python string objects.
You can now confidently access different characters in a string using indices
and slices, and you can determine the length of a string with len()
.
You also practiced methods. You used .upper()
and .lower()
to convert all characters of a string to uppercase and lowercase,
respectively. With .rstrip()
, .lstrip()
, and .strip()
, you removed
whitespace from strings, and you figured out if a string starts or ends
with a given substring through .startswith()
and .endswith()
.
You also practiced capturing input from a user as a string using the
input()
function, and you converted that input to a number using int()
and float()
. You converted numbers and other objects to strings using
str()
.
Finally, you used the .find()
and .replace()
methods to find
the location of a substring and replace a substring with a new string.
For more on these concepts, check out the following video courses:
- Reading Input and Writing Output in Python
- Python 3’s F-Strings: An Improved String Formatting Syntax
- Splitting, Concatenating, and Joining Strings in Python
- Check if a Python String Contains a Substring
- Replacing a String in Python
You can also read these tutorials:
- Basic Input, Output, and String Formatting in Python
- Python’s F-String for String Interpolation and Formatting
- Splitting, Concatenating, and Joining Strings in Python
- How to Read Python Input as Integers
- How to Check if a Python String Contains a Substring
- How to Replace a String in Python
To continue your Python learning journey, check out the other Python Basics courses. You might also consider getting yourself a copy of Python Basics: A Practical Introduction to Python 3.
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
Martin Breuss RP Team on Sept. 30, 2024
@emmanuel I just tried running the following on repl.it and it worked as expected for me:
print("Hello \
there")
This is a core Python syntax feature, so it should work anywhere you’re running Python, also in an online IDE such as repl.it
If you want, you can share the repl and we can try to debug why it isn’t working.
Either way, you found some other ways of doing the same thing that are overall better readable than using the backslash character, so nice work on that :)
For anyone wanting to dive deeper, you can read more in the following tutorials:
And finally, like you mentioned, when you use indexing to get a single element out of an iterable (e.g. a character from a string), then you only need to pass its index into the square brackets.
You can read more in indexing and slicing strings.
emmanuel on Sept. 30, 2024
Hello, Thank you again for your feedback! I must have done something wrong as i just copied & pasted what you wrote and it works… Btw, I dont know if you can acces my replit for this … I would be happy to see if it is possible and if you can write something in it … replit.com/@emmanuelgoitia/RealPython-StringExercices?v=1
Martin Breuss RP Team on Oct. 1, 2024
@emmanuel I can view it and fork it, then run it on my own account. I can’t directly edit yours. This is how repl.it is set up to work.
emmanuel on Oct. 2, 2024
Hello Martin, I must admit I am a total newbie in most things related to Python and its use. I purchased this mainly to do the 100 days code challenge and run python on a machine without installing it .... Is there a tutorial on this on Real python by the way or on the debugger ?
Martin Breuss RP Team on Oct. 4, 2024
@emmanuel why don’t you want to install Python on your machine? It’s not a big trouble and if you’re serious about learning Python, I’d recommend that you install it.
You can do a lot of experimentation and coding also in online environments such as repl.it, and you’re probably fine for a while. But you’ll probably bump into some situations that are specific to that tool, or into challenges that aren’t there (or are different) when you program locally.
Anyways, we don’t have a specific tutorial about programming in Python without installing it locally, but most of everything you can learn here will also apply : )
If you’re looking into tutorials about debugging, we do have a couple on the site. You can also use the search interface to find them, e.g. realpython.com/search?kind=article&q=debugging. Hope that helps!
Become a Member to join the conversation.
emmanuel on Sept. 29, 2024
Hello/Hallo, I really liked this course .. I have questions as some of the solutions did not work for me (but i did not use repl, i used something named replit)
Exercice 4 of Task 1, with \ does not work… This below works :
Also when getting character from s string i thought it was mandatory to write
user_input[0:1]
but when I saw that you wrote[0]
… Anyway, i realised that you could chain methods and its a must ! Vielen Dank noch einmal!