Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Python Basics Exercises: Strings and String Methods (Overview)

In Python Basics: Strings and String Methods, you used strings for text data in Python. You also learned how to manipulate strings with string methods. For example, you changed strings from lowercase to uppercase, removed whitespace from the beginning or end of a string, and replaced parts of a string with different text.

In this video course, you’ll practice:

  • Manipulating strings with string methods
  • Working with user input
  • Dealing with strings of numbers
  • Formatting strings for printing

This video course is part of the Python Basics series, which accompanies Python Basics: A Practical Introduction to Python 3.

Note that you’ll be using IDLE to interact with Python throughout this course. If you’re just getting started, then you might want to check out Python Basics: Setting Up Python before diving into this course.

Download

Sample Code (.zip)

12.8 KB
Download

Course Slides (.pdf)

6.8 MB

00:00 Welcome to this Real Python Exercises course, where you’ll practice working with strings and string methods. Our exercises courses are all about training.

00:09 You’ll train the process of writing code by solving carefully selected exercises. You’ll also train reading other people’s code and communicating your thought process.

00:18 Doing all that, you’ll practice the concepts that you’ve learned about in an associated course or tutorial and help make them stick.

00:25 In the upcoming lessons, I’ll introduce you to tasks, give you an opportunity to solve them yourself, and then show you step by step how I solved each of them.

00:32 You’ll go through three steps for each task: learn about the exercise, code your own solution, and then compare your solution and the process that got you there to mine.

00:43 When I walk you through a task, I’ll explain what I do and also why I do it like that. That’ll give you a chance to compare not just our final solutions, but also how we got there.

00:53 Ideally, this can help you gain some insights on the process of getting from a task description to a working solution in code.

01:00 This course consists primarily of short exercises that you can tackle one after another. You’ll learn to work with strings, modify strings with string methods, collect user input, work with strings and numbers, use more string methods, and finally, you can apply your knowledge in a little challenge where you’ll write a program to convert your user’s input into leetspeak.

01:24 The idea for this exercises course is that you should have watched the Python Basics course on strings and string methods before starting this one. If you went through that course, then you’re well equipped to solve the tasks that you’re about to encounter.

01:38 The concepts that you should have heard about and will practice are Python strings, string methods, user input collection, and type conversion. If you’re already somewhat familiar with these concepts, but you want to fortify your knowledge with practical programming tasks, then this course is exactly right for you.

01:57 Before you get started, there’s another tiny bit of background for this course, which is that I’ll use IDLE, the Integrated Development and Learning Environment that comes with Python.

02:06 If you’ve gone through the Python Basics courses, then you’re already familiar with the tool. If not, and you want to know more, then you can check out these associated courses that cover getting started with IDLE.

02:16 But I won’t use any specific features of IDLE. So if you’re here to train outside of the Python Basics course, then feel free to use whatever tool you like to solve the upcoming coding tasks.

02:28 And that’s all there is to say to get you set up. If you’re ready to get started and do hands-on programming, then see you in the next lesson, where you’ll start with the first review exercise.

02:37 Are you ready to get started? Hmm. I didn’t hear anything. Could you speak up, please? All right, that’s better. Let’s go.

emru67 on Jan. 17, 2024

Given three variables:

string1 = "Animals"
string2 = "Badger"
string3 = "Honey Bee"

the only way I found to put the values in a list is this:

values_list = [globals()[f'string{i}'] for i in range(1,4)]

Is it good practice to use globals() like that ? If not, how to achieve what I’m trying to do?

Thanks

Bartosz Zaczyński RP Team on Jan. 17, 2024

@emru67 Generally, it’s not considered a good practice to use globals() in Python. One reason for that is it introduces a dependency on your variable names. So, if you change those names at some point, you’ll break the code without noticing until you actually run the program and see a runtime error.

Can you describe what you’re trying to achieve on a higher level? There might be a more elegant way to approach your problem.

Martin Breuss RP Team on Jan. 17, 2024

@emru67 if you just want to put the values into a list, then you can do this:

string1 = "Animals"
string2 = "Badger"
string3 = "Honey Bee"

values_list = [string1, string2, string3]

But you might be after something else? You’ll learn more about lists in a later course in this learning path.

emru67 on Jan. 18, 2024

Thanks for the prompt answers. What I’m trying to understand is how to go through any variables present in a script without knowing their names in advance and without having to hard code them by hand. It’s just something I don’t feel I grasped fully. Originally the question raised because I imagined a scenario where the variables names are system produced, there may be two or twenty (es.: string1, string2, string(n)…). The solution I found doesn’t feel correct, but I couldn’t think of anything else. I already completed the List course, that’s why I know about list comprehensions. I apologize for not making my point clearer. Thanks

Bartosz Zaczyński RP Team on Jan. 18, 2024

@emru67 Can you clarify this part, please?

I imagined a scenario where the variables names are system produced,

I can’t think of a real-life use case for this. Data produced by some kind of system should be separate from the source code that processes it. Typically, you’d load the data from a file, a database, or a web service rather than manipulate the individual variables unless you had a good reason to perform code introspection.

emru67 on Jan. 19, 2024

You are right, I cannot think of a real life scenario as well…

What happened was that I wanted to make the excercise I was doing more interesting using a loop to apply all the various string methods to the variables values (.lower(), .upper(), etc.). When I tried I found out that I couldn’t easily access the variables except for hard coding them by hand. Then it became a challenge to find a way to get to them programmatically…I learned a few things along the way which is the goal anyway…

Thanks all for your support

Martin Breuss RP Team on Jan. 22, 2024

@emru67 if you wanted to see the names that you defined in your current script or session, then you could use the globals() function and e.g. a list comprehension to see them all:

>>> string1 = "hi!"
>>> string2 = "ho!"
>>> user_defined_names = [s for s in globals() if not s.startswith("_")]
>>> user_defined_names
['string1', 'string2']

Here, you’re using a list comprehension to filter out all names that start with a single or double underscore to remove the built-in non-public names and show only the ones you defined. Assuming that you didn’t define any names with a leading underscore, that is ;P

Then, you could theoretically use that information to look them up in the globals dictionary and apply string methods on all of them programmatically:

>>> for name in user_defined_names:
...     globals()[name].upper()
...
'HI!'
'HO!'

However, this isn’t a good idea. Python allows you to do many things, but if it’s hacky to get there, then you can assume that there’s probably a reason for that. Also, doing something like that is going into Python’s internals, which is potentially a bit early when you’re working through the Python Basics course.

But on the other hand, everyone learns differently, and if this is exciting to you then I’d encourage you to keep exploring behind the scenes :))

emru67 on Jan. 24, 2024

Thanks again for the support and the clear explanation. I do like to explore and I’m a bit more “advanced” then basic string manipulation, that was a review of things done. It’s important to understand the inner workings of a language to really know what’s going on. At Real Python you always encourage experimenting to gain knowledge and it’s one of the reasons why I joined.

Thanks all for your help

Martin Breuss RP Team on Jan. 24, 2024

Yay, of course, happy to @emru67! Good idea to revisit basic concepts and dive deeper 🙌

Become a Member to join the conversation.