Interaction with the user

Module 2 · Interaction with the user · Lesson 5 of 6

String operators

Since input() hands you strings, it's worth knowing what you can do with them. Two operators you already know from numbers work on text as well — they just mean something different.

The same symbols, for text
"ab"+"cd""abcd"join
"ha"*3"hahaha"repeat

+ is concatenation: it joins two strings end to end, so "ab" + "cd" becomes "abcd". Note it adds nothing between them — if you want a space, you include one yourself.

* is replication: a string times a number repeats it, so "ha" * 3 is "hahaha" — handy for drawing separator lines. Try them:

Run it yourselfruns in your browser · Python
main.py

Both follow the same logic as before: + needs two strings (you can't join text and a number — cast first), and * needs a string and an integer. A quick quiz next to tie input, casting and strings together.