What is a String?
Concepts covered: Strings
A string is a run of characters that Python keeps in order, from first to last: the letters of a city name, the digits of a postcode, a whole paragraph of an email. You write one by placing the text between quotes. The quotes mark where the text starts and stops, and they're not stored with the value: "Oslo" holds 4 characters, the capital O and the 3 small letters after it.
city = "Oslo"
code = 'OSL'
print(city)
print(code)
print(type(city) == str)
print("Oslo" == 'Oslo')Output:
Oslo OSL True True
Python accepts either double quotes or single quotes, and the value is the same either way: the last line prints True because both literals hold the same 4 characters. The type of every string is str, and the third line confirms it: type(city) == str is True. Pick one quote style for a program and keep to it, so a reader scanning your code sees the same shape on every line.
Text that looks like a number
Putting digits between quotes makes a string. "42" is 2 characters that happen to be digits, and 42 is an integer the computer can add and multiply. They're stored differently and never compare equal, so a value that arrives from a form field or a spreadsheet cell as text stays text until something converts it. Arithmetic works on the integer and leaves the string alone.
text = "42"
number = 42
print(text == number)
print(type(text) == str)
print(type(number) == int)
print(number + number)Output:
False True True 84
| Literal | Type | What it holds |
|---|---|---|
| "42" | str | 2 characters, the digits 4 and 2 |
| 42 | int | the integer 42 |
| "" | str | 0 characters, the empty string |
| " " | str | 1 character, a space |
| "True" | str | 4 characters, a word rather than the boolean True |
A ZIP code such as "02134" must stay a string: stored as the integer 2134 it loses its leading zero and the address lookup fails. Phone numbers behave the same way, and so do product codes and account identifiers. In data work, if you would never add 2 of them together, keep them as strings.
Walking through the characters
A for loop over a string visits its characters one at a time, in order, the same way a loop over range(4) visits the numbers 0 through 3. Each pass hands the loop variable 1 character, which is itself a tiny string holding that single character. Counting the passes tells you how many characters the string has, and the animation below does that for "Oslo".
word = "Oslo"
count = 0
for ch in word:
count = count + 1
print(ch)
print("characters:", count)O s l o characters: 4
The loop body ran 4 times, so count finished at 4 and the last line printed characters: 4. Run the same loop over the empty string "" and the body never executes: count stays 0 and only the final line is printed. A space is a character like any other, so "New York" counts to 8, including the gap between the words.
print("it's a string")
print('she said "go"')Output:
it's a string she said "go"
Equal means every character matches
Two strings compare equal with == only when they hold the same characters in the same order. Python checks them one position at a time, and a capital letter is a different character from its small form, so "Oslo" == "oslo" is False. A trailing space counts too: "Oslo " has 5 characters and never equals the 4-character "Oslo", even though both look alike on screen.
print("Oslo" == "Oslo")
print("Oslo" == "oslo")
print("Oslo " == "Oslo")
print("Oslo" == "Olso")Output:
True False False False
The last line fails for a third reason: the same 4 letters in a different order make a different string. When 2 values that should match come back unequal, compare their case first. If the case matches, look for a stray space or for characters in the wrong order. Each one hides on screen until you compare the strings character by character.
What does this program print?
word = "Hi"
code = 7
count = 0
for ch in word:
count = count + 1
print(count == 2)
print(word == "hi")
print(code == "7")- True, then True, then False
- True, then False, then False
- True, then False, then True
- 2, then False, then False
Counting works the same whatever kind of characters a string holds. Every character gives the loop 1 pass, spaces and punctuation included, so the product code "X9-7" counts to 4 and the postcode "02134" counts to 5. The loop visits each character in turn without looking at what kind it is.
About this lesson
This section is part of the Strings: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.