Skip to main content

Learning-python:
Strings

image for Strings

Strings

String concatenation

Concatenation means joining strings together.

Using the + operator

Stick strings together with +:

greeting = "Hello" + " " + "World"
# Result: "Hello World"

You can also join string variables:

first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
# Result: "John Doe"

Mixing strings and numbers

If you want to include a number in your string, convert it to text first using str():

age = 25
message = "I am " + str(age) + " years old"
# Result: "I am 25 years old"

Without str(), Python gets confused because it doesn't know how to mix text and numbers.

Joining multiple pieces

You can chain many pieces together:

first_name = "Alice"
middle_name = "Marie"
last_name = "Smith"
full_name = first_name + " " + middle_name + " " + last_name
# Result: "Alice Marie Smith"

Or mix variables with plain text:

city = "London"
country = "England"
message = "I live in " + city + ", " + country
# Result: "I live in London, England"

Storing concatenated strings

Save the result in a new variable for later use:

street = "123 Main Street"
city = "London"
postcode = "SW1A 1AA"
full_address = street + ", " + city + ", " + postcode
# Now full_address contains the complete address

Different data types

Always convert non-text types to text before joining:

name = "Bob"
age = 30
message = name + " is " + str(age) + " years old"
# Result: "Bob is 30 years old"

This works because we converted age (a number) into text with str().