Beginner · Text and decisions · topic 5 of 50 · 20 min
Strings: working with text
Explanation
What is a string?
A string is a piece of text: a name, a city, a message, even a phone number written in quotes. In Python the type of a string is str. You can write a string with single quotes or double quotes. Both mean the same thing.
city = "Mumbai"
food = 'vada pav'
print(city, food)
print("It's hot today")
note = """Line one
Line two"""
print(note)Length, joining and repeating: len() tells you how many characters a string has. Spaces count as characters too. The + sign joins two strings into one; joining is also called concatenation. The * sign repeats a string a number of times.
name = "Priya"
print(len(name))
full = "Priya" + " " + "Sharma"
print(full)
print(len(full))
print("-" * 20)
print("ha" * 3)Indexing: picking one character
An index is the position number of a character. Python starts counting at 0, so the first character is at index 0. Negative indexes count from the end: -1 is the last character, -2 the one before it.
| Character | P | Y | T | H | O | N |
|---|---|---|---|---|---|---|
| Index from the start | 0 | 1 | 2 | 3 | 4 | 5 |
| Index from the end | -6 | -5 | -4 | -3 | -2 | -1 |
word = "PYTHON"
print(word[0]) # first character: P
print(word[2]) # third character: T
print(word[-1]) # last character: N
print(word[-2]) # second from the end: OSlicing: cutting out a piece
A slice is a piece of a string. You write [start:stop]. Python starts at index start and stops just BEFORE index stop. Leave out start to begin at the beginning; leave out stop to go to the end. An optional third number, the step, jumps over characters.
pnr = "4521367890"
print(pnr[0:3]) # 452
print(pnr[3:]) # 1367890
print(pnr[:4]) # 4521
print(pnr[-4:]) # 7890, the last four
print(pnr[::2]) # 42379, every second digit
print(pnr[::-1]) # 0987631254, reversedStrings are immutable. Immutable means "cannot be changed after it is made". You cannot replace one character inside a string. Instead, you build a new string and store it in the variable.
name = "ravi"
name = "R" + name[1:]
print(name)Turning numbers into text and text into numbers
str() turns a value into a string. int() turns a string of digits into a whole number, and float() turns it into a decimal number. You need these because Python never mixes text and numbers on its own.
age = 21
print("Age: " + str(age))
marks = "85"
print(int(marks) + 5)
print(float("99.5"))Watch the lesson
Strings: working with text · 1/7
In this lesson you will learn about strings. A string is text inside quotes, like a name or a city.
Prefer a video by a teacher? Find free videos on “python strings indexing slicing tutorial for beginners” on YouTube ↗
In real life: Hiding a bank account number
When Rahul checks his bank app, it shows only the last four digits of his account number and hides the rest with X. The app does this with a slice and string repeating: X times the hidden length, plus the last four characters.
account = "50100234567891"
hidden = len(account) - 4
masked = "X" * hidden + account[-4:]
print(masked)Practice (0/3 done)
Write a function initials(first, last) that returns the first letter of each name followed by a dot. Example: initials("Rahul", "Sharma") returns "R.S."
Write a function mask_number(number) that hides every character except the last 4 with X. Example: mask_number("9876543210") returns "XXXXXX3210". If the number has only 4 characters, nothing is hidden.
Write a function price_tag(item, price) that returns a sentence like "Tea costs Rs 15". item is a string and price is a whole number.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.