Unit 2 · Python mode... Activate! · Lesson 19 of 57
Stringing Things Together
Stringing things together
You already know strings are just text in quotes. But what if you want to stick two of them together?
You can add them! Same + as math:
print("Cyber" + "Acad")
CyberAcad
This is called concatenation (con-cat-en-ay-shun), which is a fancy word for gluing strings together. You can glue variables too:
first = "Zain"
second = "is the networking guy"
print(first + second)
Zainis the networking guy
...wait. Zainis?
Python glues strings together exactly as they are. It won't add a space for you, because remember, it doesn't use common sense! If you want a space, you have to add it yourself:
print(first + " " + second)
Zain is the networking guy
That " " is a string with just a space in it. Looks silly, but it works!
This is the quick and dirty way to build sentences. It works great for short stuff. For long sentences... well, you'll see.
Alex did something again.
Use the three variables and + to print:
Alex deleted the website
Don't type the sentence yourself, glue the variables together! (And don't forget the spaces.)
Python exercise
Write the program in an editor next to the lesson, run it, and have it checked automatically. Create a free account to do this part.
Do this lesson for real
With a free account you get the terminals, challenges and checks that go with the lessons in Intro to Python, and your progress is saved as you go. CyberAcad is a 501(c)(3) nonprofit, so there is nothing to pay.