Python 3D Academyবাংলা

Making strings: quotes and escapes

🌍 In real life. Everything you type in a text message is text: letters, digits, spaces, emoji, Bangla. Even a phone number in your contacts, like 01712-345678, is kept as text. You never add two phone numbers together; you only store them, show them and search them.

In Python, text is called a string, and it's one of the types you'll use most.

The idea in one sentence: a string (str) is a piece of text wrapped in quotes, and Python keeps exactly the characters between the quotes.

Its words:

  • character: one letter, digit, space or symbol
  • string: a row of characters in quotes, "double" or 'single'
  • escape: a backslash \ that changes what the next character means
  • length: how many characters a string has, given by len()

Step by step, for print('She said "hi"'):

  1. Python sees the opening ' and starts collecting characters.
  2. The " marks inside aren't the same kind of quote, so they're just characters.
  3. The next ' ends the string, and print shows: She said "hi"

Digits in quotes are text too. Watch how quotes change the type:

print("Hello")
print('Hello')
print("আমি পাইথন শিখছি")

How Python reads quotes. A string ends at the next quote of the same kind it started with. So to put a quote mark inside your text, either wrap the string in the other kind of quote, or escape the quote with a backslash.

print("It's sunny")
print('She said "hi"')

Or use an escape: a backslash \ changes what the next character means.

  • \" or \' is a quote that doesn't end the string
  • \n starts a new line
  • \t is a tab (a wide space)
  • \\ is one real backslash
print("Line one\nLine two")
print("Name:\tRimi")
print("C:\\Users\\rimi")
print('It\'s fine')

+ joins two strings, * repeats one, and len() counts the characters.

first = "Py"
second = "thon"
print(first + second)
print("ha" * 3)
print(len("hello"))

Triple quotes (""") let a string run over several lines:

poem = """Roses are red,
Python is fun."""
print(poem)

Mistakes and edge cases.

  • The same quote inside and outside. print('It's sunny') gives SyntaxError: unterminated string literal: the ' in It's ends the string early. Use "It's sunny".
  • Joining text and a number. "Hi" + 5 gives TypeError: can only concatenate str (not "int") to str.
  • Backslashes in paths. In "C:\new", the \n becomes a new line. Write \\ for one real backslash.
  • Repeating by a decimal. "ha" * 2.5 gives TypeError: can't multiply sequence by non-int of type 'float'.
  • Spaces count. len("hi there") is 8: the space is a character too.

Quiz

  1. What does this print?

    print("ab" + "cd")
  2. What does this print?

    print("-" * 5)
  3. What does this print?

    print(len("cat"))
  4. What does this print?

    print("A\nB")
  5. Which line causes an error?

    print("Good morning")
    print('It's late')
  6. What is the type of "3.5"?

    • float
    • str
    • int

Coding tasks

  1. Print exactly this:

    She said "hi"

  2. Using one print(), show two lines:

    Hello
    World
    

    Use \n.

  3. word holds "na". Make song hold "nananana batman", using word with * and +.