Joining and repeating strings
You've joined strings with + already. Python calls this concatenation: the strings are put one after the other, exactly as they are. Nothing is added between them, so any space has to be inside one of the strings:
first = "sun"
second = "flower"
print(first + second)
print(first + " " + second)
sunflower
sun flowerRepeating a string with *
The * operator works on strings too, when the other side is a whole number: it repeats the string that many times.
print("ha" * 3)
print("-" * 20)
print("=" * 5 + " Menu " + "=" * 5)
hahaha
--------------------
===== Menu =====As with numbers, * comes before +, so on the last line Python first repeats the two = strings and then joins the three pieces.
The number can be a variable, which makes * handy in loops. This loop draws a triangle, one # wider in every round:
width = 1
while width <= 4:
print("#" * width)
width += 1
#
##
###
####Numbers from input
A string can only be repeated a whole number of times. input always gives a string, even when the user types digits, so forgetting int stops the program:
width = input("Width: ")
print("*" * width)
6Width: 6Traceback (most recent call last):
File "main.py", line 2, in <module>
print("*" * width)
~~~~^~~~~~~
TypeError: can't multiply sequence by non-int of type 'str'A sequence is Python's word for a value that holds things in order, as a string holds its characters. The message says that * got a string on its other side ('str') where it needs an int. With width = int(input("Width: ")) the program works.