Playing with text and words
Learn how to work with text in Python โ join words, repeat them, slice them up, and more!
A string is just a piece of text. It could be a word, a sentence, or a whole book. In Python, you wrap text in quotes to make a string. Then you can join strings together, repeat them, measure them, and much more!
๐ก Think of it like this:
Think of a string like a bead necklace. Each letter is a bead. You can count the beads, add more beads, or take some off. The whole necklace is the string!
Almost every program works with text โ names, messages, passwords, stories. Understanding strings is essential for building chat apps, games with dialogue, and anything that uses words.
Where you see this in real life:
The longest word in English is 45 letters long: "pneumonoultramicroscopicsilicovolcanoconiosis". Python can handle it easily with len()!
A string is a piece of text. It can be a word, a sentence, or even a whole paragraph. In Python, you wrap text in quotes to create a string.
You can use single quotes 'like this'or double quotes "like this" โ both work the same way!
You can join strings together using the + operator. This is called concatenation (a fancy word for "joining")!
Click โถ Run to see the output!๐ก Notice we added " " (spaces) between words. Without them, it would say "Pythonisawesome!"
You can repeat a string using the * operator. It is like saying "repeat this text N times!"
Click โถ Run to see the output!The len()function tells you how many characters are in a string (including spaces!).
Click โถ Run to see the output!You can grab parts of a string using slicing. Use square brackets with a start and end position. Remember: Python starts counting from 0!
Click โถ Run to see the output!What will print('Na' * 4) output?