Concatenating Strings with the +
Operator
The simplest way to concatenate strings is by using the '+' operator.
The '+' operator can only be used between strings. If you attempt to use it with non-string data types, it will result in a type error. To combine strings with other data types, you must first convert the other data types to strings.
String Concatenation Example
greeting = "Python " + "Programming!"
print(greeting) # "Python Programming!"
# Convert a number to a string for concatenation
age = 30
message = "I am " + str(age) + " years old."
print(message) # "I am 30 years old."
Characteristics of the +
Operator for Strings
-
Simplicity
: The '+' operator is useful for keeping your code concise while joining strings. -
String Only
: The '+' operator applies only to string data types.
Want to learn more?
Join CodeFriends Plus membership or enroll in a course to start your journey.