โ† Back to Python Course
๐Ÿ Python Playgroundยท20 minยทBeginner
๐Ÿ“

Strings

Playing with text and words

Learn how to work with text in Python โ€” join words, repeat them, slice them up, and more!

๐Ÿ“–

What we are learning

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!

๐ŸŒŸ

Why it matters

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:

  • โœ“Chat apps send and receive string messages
  • โœ“Games display dialogue and story text using strings
  • โœ“Search engines look for strings inside web pages
๐ŸŽฏ

By the end, you can:

  • 1.Create strings with single and double quotes
  • 2.Join strings together with +
  • 3.Repeat strings with *
  • 4.Find the length of a string with len()
๐Ÿ”ค

New words

stringconcatenationindexlen
๐Ÿ
If you could make Python repeat any word 100 times, which word would you pick?
๐Ÿค“

Fun Fact!

The longest word in English is 45 letters long: "pneumonoultramicroscopicsilicovolcanoconiosis". Python can handle it easily with len()!

๐Ÿ“ What is a String?

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!

๐Ÿ”— Joining Strings

You can join strings together using the + operator. This is called concatenation (a fancy word for "joining")!

๐ŸJoin strings with +
main.py
โ— Output
Click โ–ถ Run to see the output!

๐Ÿ’ก Notice we added " " (spaces) between words. Without them, it would say "Pythonisawesome!"

๐Ÿ” Repeating Strings

You can repeat a string using the * operator. It is like saying "repeat this text N times!"

๐ŸRepeat strings with *
main.py
โ— Output
Click โ–ถ Run to see the output!

๐Ÿ“ Measuring Strings

The len()function tells you how many characters are in a string (including spaces!).

๐ŸCount characters with len()
main.py
โ— Output
Click โ–ถ Run to see the output!

โœ‚๏ธ Slicing Strings

You can grab parts of a string using slicing. Use square brackets with a start and end position. Remember: Python starts counting from 0!

๐ŸSlice strings with [ ]
main.py
โ— Output
Click โ–ถ Run to see the output!
๐Ÿ
Try this: create a variable with your name, then print only the first 3 letters of it!
๐Ÿง 

Quick Quiz!

+10 XP

What will print('Na' * 4) output?

๐Ÿ
Finished the lesson? Click the button to mark it complete and earn XP!