Skip to content
IT Vizag
← Python Basics

▶ Sign up free for the video, slides & prompts

The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.

Checking…

Working with text (strings)

Lesson 6 of 10 · 1 min read

Join, slice and format text with f-strings.

🎬 Video version coming soon — read it now

Text in Python is called a string (str): characters inside quotes.

🔒 Code example: sign up free (above) to unlock it.

Joining strings

🔒 Code example: sign up free (above) to unlock it.

You can't add a string and a number directly. "Age: " + 21 gives a TypeError. Turn the number into text first with str(21), or use an f-string.

f-strings: the best way to mix text and values

Put an f before the quote and wrap variables in { }:

🔒 Code example: sign up free (above) to unlock it.

:.1f means "show one decimal place". You'll use f-strings in almost every program you write.

Length, indexing and slicing

🔒 Code example: sign up free (above) to unlock it.

Handy string methods

🔒 Code example: sign up free (above) to unlock it.

Methods return a new string. The original doesn't change, because strings can't be changed in place. To keep the result, store it: text = text.strip().

Tip: Python's own help is built in. Run help(str.split) in Colab to read about any method.

In one line

Strings hold text; join them with +, build them with f-strings, and slice them with [start:end].

Saved on this device. Sign in to keep progress everywhere.

Share with a friend