Python: String Concatenation
By ChatGPT, proofread by AMO
Note: In the following post, the responses are those of ChatGPT after being informed of a basic script.
In Python, you can concatenate strings in a print
statement using the +
operator or by using the format()
method or string interpolation (f-strings).
Here are some examples:
1 Using the +
operator:
a = "Hello"
b = "world"
print(a + " " + b) # Concatenation using the + operator
2 Using the format() method:
a = "Hello"
b = "world"
print("{} {}".format(a, b)) # Using the format() method
3 Using f-strings (string interpolation):
a = "Hello"
b = "world"
print(f"{a} {b}") # Using f-strings
All three methods produce the same output: “Hello world”. You can choose the one that suits you best based on your personal preference and the Python version you are using (f-strings are available from Python 3.6 onwards).