Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Converting | Strings
Introduction to Python | Mobile-Friendly
course content

Kursusindhold

Introduction to Python | Mobile-Friendly

Introduction to Python | Mobile-Friendly

1. Python Basics
2. Variables
3. Strings

book
Converting

Good! But what if you want to concatenate a string with a numerical value? Like, Level: 5. If you try to apply the same approach as in the previous chapter, you will get an error that you can not concatenate str and int types.

So, how do you avoid this problem? You cannot convert text into a numerical type, but you can do the opposite. To convert a variable into a string type, use the str() function, putting the value/variable you want to convert within the parentheses. For instance, let's represent the example above in the code.

12345
# String and numerical values string = "Level: " number = 5 # Output result of the concatenations print(string + str(number))
copy

Note that we left a blank space to the right of the colon sign within the string variable.

The variables chapter and section represent the current chapter position. You need to output the message using the print() function two times in the following format:

python
question-icon

Fill in the blanks to complete the task.

# Variables
section = 3
chapter = 3

# Output the message
print()
print(
)
Section 3
Chapter 3

Click or drag`n`drop items and fill in the blanks

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 3

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

course content

Kursusindhold

Introduction to Python | Mobile-Friendly

Introduction to Python | Mobile-Friendly

1. Python Basics
2. Variables
3. Strings

book
Converting

Good! But what if you want to concatenate a string with a numerical value? Like, Level: 5. If you try to apply the same approach as in the previous chapter, you will get an error that you can not concatenate str and int types.

So, how do you avoid this problem? You cannot convert text into a numerical type, but you can do the opposite. To convert a variable into a string type, use the str() function, putting the value/variable you want to convert within the parentheses. For instance, let's represent the example above in the code.

12345
# String and numerical values string = "Level: " number = 5 # Output result of the concatenations print(string + str(number))
copy

Note that we left a blank space to the right of the colon sign within the string variable.

The variables chapter and section represent the current chapter position. You need to output the message using the print() function two times in the following format:

python
question-icon

Fill in the blanks to complete the task.

# Variables
section = 3
chapter = 3

# Output the message
print()
print(
)
Section 3
Chapter 3

Click or drag`n`drop items and fill in the blanks

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 3. Kapitel 3
Vi beklager, at noget gik galt. Hvad skete der?
some-alt