CONCATENATION
In Python, concatenation refers to the process of joining two or more strings together to form a single string. Strings can be concatenated without a space using the + operator directly, for example, "Good" + "Work" results in "GoodWork". To include a space between concatenated strings, you can use a comma in the print() function, which automatically inserts a space (e.g., print("Hello", "World") outputs Hello World), or explicitly add a space with the + operator (e.g., "Hello" + " " + "World" also outputs Hello World). It’s important to note that concatenation works only with string data types; combining a string with a number directly will cause an error, so numbers must be converted to strings first using str(). Concatenation is a fundamental operation in Python for building dynamic messages, formatting output, and combining text data.
W/O SPACE
#W/O SPACE IN-BETWEEN
x = "Good"
y = "Work"
print(x + y) #output will be: GoodWorkWITH SPACE
#WITH SPACE IN-BETWEEN
#!/usr/bin/env python3
def main() -> None:
variableX = "Hello";
variableY = "World";
#METHOD 1
print(variableX, variableY); #space is automatically added when using the comma
#METHOD 2
print(variableX + " " + variableY);
if __name__ == "__main__":
main();
* OUTPUT
Hello World
Hello WorldLast updated