Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Strings Concatenation | Basic Concepts
String Manipulation in Python

book
Strings Concatenation

What if we need to connect two strings? Like, we have a price as a number in price variable, and we want to make it representatively, i.e. we want to show the price in format n $. This problem can be solved by concatenation.

Concatenation is joining two strings, which can be done in Python by using a simple + operator. It will join the string on the left side of the operator to the string on the right side. For example,

price = 25
print('Price is', str(price) + '$')
12
price = 25 print('Price is', str(price) + '$')
copy

As we can see, + operator added the '$' sign to the price converted into a string.

Note

All the space characters are considered by + operator. For example, if in the example above we wrote str(price) + ' $' it would output Price is 25 $.

Завдання

Swipe to start coding

Given variables height and weight. Using concatenation, output the following messages:

Height: 170 cm

Weight: 70 kg

Рішення

# Variables
height = 170
weight = 70

# Output using concatenation
print("Height:", str(height) + ' cm')
print("Weight:", str(weight) + ' kg')

Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 8
# Variables
height = 170
weight = 70

# Output using concatenation
print("Height:", str(height) + ___)
print("___", ___(weight) ___)
toggle bottom row
some-alt