Level Four: Strings in Python

Let’s learn how strings work in Python!

Remember the messages we printed on the first level, for example: “Hello! This is my first line of code!”

Well, those are all strings.

In Python, a string is a data type that holds text. It’s simply a text that we enclose in quotes.

So, to create a string, we type some text and put it in quotes.

Strings and The Print Function

Let’s write a code that would display the following string: “Now I know how strings work!”

You already know how to print messages to the screen in Python, right?

Here is the code to get started. Type the below code into the compiler and run it.

print("Now I know how strings work!")
Code
Run
#write code here

Now it’s your turn.

Come up with a different string, rewrite and run the code above.

Storing Strings in Variables

Do you remember our first variable?

car = "Tesla"
print(car)

Now you know that the value of the variable – “Tesla” had a string format, because we enclosed this value in quotes.

With that said, let’s train our string muscles with this small task:

We will create a message in a string format. Then, we will save it in a variable and display the value of the variable on the screen using the print function.

Let me do it first:

message = "Houston, we have a problem"
print(message)

Type the above code into the compiler and run it.

Code
Run
#write code here

Did you manage to display the message? Great!

Now it’s your turn.

Looking at the example above, come up with your own message that we can store in a variable. And then display the value of the variable on the screen.

Come up with as many examples as you like! The more, the better. This way you will train your coding skills faster!

Strings Concatenation in Python

We can combine different strings with each other. This technique is called “concatenation”.

All we need to do to concatenate strings is to put the addition operator + between them.

Nothing complicated, right? Now let’s get practicing!

print("Elon Musk sent Tesla to" + "Mars")

Type the above code into the compiler and run it.

Code
Run
#write code here

Hmm, something is off here. It seems like our strings are stuck together.

No problem! We can easily fix that. There are several ways. Here is the simplest one:

All we need to do is leave a space between the first quote of the second string and the word that follows that quote:

print("Elon Musk sent Tesla to" + " Mars")
Code
Run
#write code here

Fixed it? Let’s run the code again 🙂

Strings Concatenation and Variables in Python

We can only concatenate a string with another string. Or with another value that has a string format.

For example, if we create a variable and give it a string format value, then we can combine such a variable with another string.

In the example below, I created a variable (brand) and assigned it the string value “Tesla”.

Then I output this value in concatenation with another string “The car is called”.

And this is what we’ve got:

brand = "Tesla"
print("The car brand is " + brand)

Type this code into the compiler and run it.

Code
Run
#write code here

If you did everything correctly, the program will return the message “The car brand is Tesla”.

Now have some practice.

Change the code as you wish. You can even combine more than two strings!

Strings Formatting in Python

Now let’s talk about string formatting.

We have learned how to concatenate strings using the mathematical operator +. This operator can only concatenate a string with another string.

But what if we want to concatenate a string with something that doesn’t have a string format?

There is a way to do that! And it’s called “String Formatting”.

Let’s concatenate a string with a variable. To do this, we will convert the value of our variable to a string format. And, to accomplish this task, we need two things:

First is the format() method for formatting a non-string value and placing it inside a placeholder string.

The second is the placeholder itself – {} for a non-string value.

Let me show you how it works in the example below:

print("My name is Joe, and I am {} years old".format(20))
Code
Run
#write code here

Enter this code into the compiler and run it.

If you did everything correctly, the program will return the message: My name is Joe, and I am 20 years old.

Did it work? Great!

Now let’s break it down:

We inserted a placeholder into our string. You can recognize this placeholder by the curly braces – {}.

We use this placeholder to store Joe’s age, which has a numeric value.

At the end of the string, we place the format() method, and pass the age itself – 20, which has a numeric value, into its parentheses.

As a result, Python took our numeric value, formatted it into a string, and placed it in the placeholder.

And finally, after converting the numeric format to a string, we output the entire string to the screen using the print function.

That’s it. Nothing too hard, right?

Practicing Strings Formatting in Python

I highly recommend that you play with the above code. Experiment with placeholders and the values you pass to the format() method.

Here is a more complex example, with two placeholders:

print("My name is {}, and I am {} years old".format("Joe", 20))

Type this example into the compiler and run it.

Code
Run
#write code here

Now have some practice and try explaining yourself how it works. Then come up with your own variation with two or more placeholders.

Level Four Quiz

You have to pass the quiz to complete Level Four!

Level Four Quiz

1 / 3

What is a string?

2 / 3

How do we create a string?

3 / 3

We ran the code and got: Hello, John Doe! Here is the first line of that code: message = "Hello, John Doe!". What was the second line of the code?

Your score is

The average score is 80%

0%

Wrapping Up Level Four

In the Level Four, you accomplished the following:

  1. Learned what strings are in Python;
  2. Learned how to store strings in a variable;
  3. Learned how to concatenate strings in Python;
  4. Learned how to concatenate strings with variable values;
  5. Learned what string formatting is and how to convert data types to string format.

Great job! Let’s move on to Level Five: Boolean Logic in Python.


Python Online is an online compiler where you can write, run, and check your code without installing any software.