CHAPTER FOUR: STRINGS

1 Strings in Python

Do you remember the messages we displayed in chapter one, like "Hey! This is my first line of code!" or "Once upon a time.." They are all strings. In other words, a string is a data type that has a text format, or just a text, enclosed in quotes. Hence, to create a string, we have to type a text and enclose it in quotes.

2 Strings And Print Function

Let's write a code that would display the string – "Now I know what a string is in Python." You already know how to display strings in Python, right? Here is the code.


print("Now I know what a string is in Python.")


Write the above line into the console and hit run. Now it's your turn. Think of some different string, rewrite and run the above code.

3 Saving Strings in Variables

Do you remember our first variable: film = "Super Cat"? Now you know that the value of the variable – "Super Cat" had a string format, as we enclose it in quotes. Let's take our knowledge further, create a message in a string format, save it in a variable, and display the variable value on your screen using the print function. Let me do it first:


message = "Hello, Super Cat!"

print(message)


Run this code in the console. What do you see? Now it's your turn. Following the above example, think of any message. Right it down as a string. Save it in a variable and display the variable value using the print function. What you've got?

4 Strings Concatenation

We can combine strings. There is a method for that, called – concatenation. All we have to do to concatenate strings is to put addition operator + between them. Easy right? If so, let's get concatenating!

"Super Cat" + "saves the day!"

Now, let's display the two concatenated strings:


print("Super Cat" + "saves the day!")


Write this code into the console and hit run. Have you noticed something weird? Hmm. It looks like our lines stuck together. No worries. We can quickly fix that. There are several ways to do it but let's stick with the most simple – leave a space between the quote and string:


print("Super Cat" +" saves the day!")


Have you fixed that? Hit run again.

5 Strings Concatenation And Variables

Note that we can concatenate a string only with another string. Or a string-formatted value. For example, if we created a variable and assigned a string-formatted value to it, we can also concatenate such a variable with another string. In the example below, I created a variable (hero) and assigned a string-formatted value (Super Cat) to it. Then I displayed this value in concatenation with another string (VS Duper Dog).


hero = "Super Cat"

print(hero + " VS Duper Dog")


Try this code out. What message do you get? Now, change the code as you like. You can even concatenate more than two strings! Alter and run the code. See how the output changes!

6 String Formatting

Now let's talk about what string formatting is. We have already learned how to concatenate strings using the + operator. The + 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? In this case, we use string formatting to turn a value that doesn't have a string format into a string-formatted value. To do so, we need two things:

The format() method to format the non-string value and insert it inside the string's placeholder.

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

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


print("My name is Super Cat, and I'm {} years old".format(2))


Please write this code into the console and run it. Here is the result you should get:



Now let's break it down.

1) We created a placeholder in our string. You can identify it by the curly brackets – {}. This placeholder is for the age of the Super Cat, which has a numeric value.

2) After the string end, we put the format() method and passed our numeric value, 2, to its brackets.

3) As a result of this operation, Python took our numeric value, formatted it into a string value, and passed it to the placeholder. So now it belongs to the entire string in the same string format.

4) After that, we displayed the entire string using the print function.

I encourage you to play with the given code and experiment with placeholders and values you pass to the format() method. Here is another way to accomplish the above exercise:


print("My name is {}, and I'm {} years old". format("Super Cat",2)


Can you explain how it works now? Can you come out with your ideas?

7 Wrapping Up Chapter Four

In chapter four, we have accomplished the following:

1) Learned what are strings in Python;

2) Learned how to store a string in a variable;

3) Learned how to concatenate strings;

4) Learned how to concatenate strings with variables values;

5) Leaned string formatting.

8 Chapter Four Test

1. What is a string?

1) A string is a line that goes through the code.

2) A string is text data enclosed in quotes.

3) A string is an integer formatted value.

4) A string is a float formatted value.

2. How do we create a string?

1) We write a text and enclose it in curly braces.

2) We write a text and enclose it in parentheses.

3) We write a text and enclose it and enclose it in quotes.

3. Arrange the code so that you get a variable with a value as a string. And then display the value of the variable on the screen.


(message)

message

"Hello, John Doe!"

print

=

Загрузка...