1 KiB
1 KiB
title | tags | aliases | ||||
---|---|---|---|---|---|---|
stringConcatenation |
|
|
[!note] String Concatenation
The+
operator, when used with two strings, combines them into a single new string — this is called string concatenation.[!example] Examples
'Alice' + 'Bob' # AliceBob 'Alice' + 42 # TypeError: can only concatenate str (not 'int') to str 'Alice' + str(42) # Alice42 'Alice' * 5 # AliceAliceAliceAliceAlice name = "Al" print("It is good to meet you, " + name) # It is good to meet you, Al
[!important] Best Practices
- Ensure both operands are strings before using
+
- Prefer
f-strings
orformat()
for cleaner code when mixing typesf"Alice{42}" # Alice42
[!info] Related Notes
typeConversions
expressions
len()
The print() Function
pythonSyntaxOverview