Strings and Booleans
15 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)Text and decisions form the foundation of every application. Python represents text with str and truth values with bool.
String literals and quotes
String (str) is the type for text. You write it between single or double quotes, and it can hold letters, digits, spaces and symbols.
In Python, you can enclose strings in single quotes '...' or double quotes "...". Both create identical string objects.
city = 'Bengaluru'role = "Software Engineer"print(city)print(role)Example 1 · A quote inside text
message = "It's ready"print(message)print(type(message))It's ready <class 'str'>
What this code does
- Double quotes let the text contain a single quote.
typeshowsstr, because the value is text.
Using double quotes lets you include single quotes inside the text (such as "It's ready") without escaping.
Multiline text with triple quotes
When you need text spanning multiple lines or docstrings, enclose it with triple quotes """...""".
message = """Invoice Summary:- Status: Paid- Total: 4500"""print(message)Triple-quoted strings preserve line breaks and indentation exactly as typed.
Boolean type: True and False
Boolean (bool) is the type with exactly two values, True and False. Both are written with a capital first letter.
Python's bool type has exactly two values: True and False. Notice that both start with a capital letter.
is_admin = Truehas_passed = Falseprint(is_admin)print(type(is_admin))Writing lowercase true or false produces a NameError because Python is case-sensitive.
Truthiness: truthy and falsy values
Truthiness is whether a value counts as True or False when Python needs a yes or no answer. Zero, empty text and None count as False; most other values count as True.
Every object in Python has an inherent boolean truth value. Use bool() to evaluate an object's truthiness.
print(bool(0))print(bool(42))print(bool(""))print(bool("MentorNxt"))Example 2 · Checking an account
user_name = "Priya"is_verified = Truelogin_attempts = 0
print(user_name)print(is_verified)print(bool(login_attempts))print(bool(user_name))Priya True False True
What this code does
user_nameholds a string.is_verifiedholds a booleanTrue.login_attemptsis0, which evaluates toFalse.user_nameis non-empty, which evaluates toTrue.
Example 3 · The text "False" is still true
flag_text = "False"print(bool(flag_text))print(bool(""))True False
What this code does
- Any non-empty string is truthy, whatever it says.
- Only the empty string
""is falsy. - To get a real boolean, write
Falsewithout quotes.
Falsy values include 0, 0.0, empty strings "", empty lists [], and None. Everything else is truthy.
Next step
Check your understanding
9 quick questions