Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære String Concatenation in Python | Variables and Types in Python
Introduction to Python(ihor)

bookString Concatenation in Python

Concatenation is the process of joining two strings together. You can concatenate strings using the + operator, just as you would add numbers. For instance, you can combine words with an article, but concatenation does not automatically insert spaces between the strings you are joining.

123456789
# Article and word article = "The" country = "Netherlands" # Concatenate without a space print(article + country) # Concatenate with a space between print(article + " " + country) # Add blank space between
copy

You can only concatenate strings with other strings. This means you can't directly concatenate a word like 'word' with a number like 1. To do so, convert the number to a string using str(1) before concatenating.

123456789
# Variables to store the greeting message and user ID greeting = "Welcome, user #" user_id = 42 # Attempting direct concatenation (will raise an error) # print(greeting + user_id) # Correct way: Convert the number to a string print(greeting + str(user_id))
copy
question mark

What happens if you concatenate a string and an integer directly?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 10

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

Still meg spørsmål om dette emnet

Oppsummer dette kapittelet

Vis eksempler fra virkeligheten

Awesome!

Completion rate improved to 1.67

bookString Concatenation in Python

Sveip for å vise menyen

Concatenation is the process of joining two strings together. You can concatenate strings using the + operator, just as you would add numbers. For instance, you can combine words with an article, but concatenation does not automatically insert spaces between the strings you are joining.

123456789
# Article and word article = "The" country = "Netherlands" # Concatenate without a space print(article + country) # Concatenate with a space between print(article + " " + country) # Add blank space between
copy

You can only concatenate strings with other strings. This means you can't directly concatenate a word like 'word' with a number like 1. To do so, convert the number to a string using str(1) before concatenating.

123456789
# Variables to store the greeting message and user ID greeting = "Welcome, user #" user_id = 42 # Attempting direct concatenation (will raise an error) # print(greeting + user_id) # Correct way: Convert the number to a string print(greeting + str(user_id))
copy
question mark

What happens if you concatenate a string and an integer directly?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 10
some-alt