Day1: String Concatenation in Python: Building Longer Strings
?? What is String Concatenation?
String concatenation is the process of joining two or more strings together using the + operator in Python.
For example:
print("Hello" + "World")
Output:
HelloWorld
In this example, the + operator is used to combine the two words Hello and World.
Example 2:
first_name = "Muhammad"
last_name = "Hanzala"
full_name = first_name + " " + last_name
print(full_name)
Output:
Muhammad Hanzala
In this example, the + operator is used to combine the first_name, a space (" "), and the last_name, creating a full name.
?? Why String Concatenation is Important:
age = 25
message = "I am " + str(age) + " years old."
print(message)
Output:
I am 25 years old.
?? Challenges with String Concatenation:
age = 30
# Will throw an error without str()
message = "I am " + age + " years old."
?? Pro Tip:
In Python, you can also use f-strings (formatted string literals) for cleaner, more efficient string concatenation, especially when combining multiple variables.
name = "Hanzalah"
age = 24
message = f"My name is {name} and I am {age} years old."
Output:
My name is Hanzalah and I am 24 years old.
Understanding how to effectively concatenate strings opens up a lot of possibilities for creating dynamic, meaningful text in your Python programs.
Stay tuned as I dive deeper into Python during my #100DaysOfCode challenge! ??
#PythonJourney #LearnToCode #PythonBootcamp #CodingLife #ProgrammingTips #CodeNewbie #StringConcatenation
Well done, Muhammad Hanzala! Amazing progress! Keep reaching new heights! ????