Salvatore Lopiparo

Senior Software Engineer

Super Simple Python #2 – Intro to Strings

While it is quite fun to use strings to play with kittens, it’s even more fun to use strings to play with Python! (Although far less cute.) A string in programming speak is an ordered series of characters, which is a fancy way of saying “words”. Let’s play with some strings!

If this is your first time here, I recommend looking at the first lesson. Starting there and going through the rest of the lessons will prepare you to go through this more advanced lesson!

To write a string, take any set of characters and put either double or single quotation marks around it:

1
2
3
4
>>> 'hi'
'hi'
>>> "howdy"
'howdy'

To Python, there is no difference between using single quotes (') and double quotes (" or Shift+'), as long as you use the same one for that string. Nobody likes mismatched bookends. (Not sure that’s a good example, as nobody uses bookends to begin with.)

We can also assign strings to a variable, just like numbers!

1
2
3
>>> hi_string = 'hi'
>>> hi_string
'hi'

and even add them!

1
2
>>> hi_string + 'howdy'
'hihowdy'

Let’s add a space between those two:

1
2
>>> hi_string + ' ' + 'howdy'
'hi howdy'

We can even type whole sentences, punctuation and all:

1
2
3
>>> taunt = "Your mother was a hamster and your father smelt of elderberries!"
>>> taunt
'Your mother was a hamster and your father smelt of elderberries!'

If we need to use an apostrophe (also known as the single quotation mark, ') in the middle of our sentence, we can either use the double quotation marks:

1
2
>>> "It's a wonderful day in the neighborhood."
"It's a wonderful day in the neighborhood."

or we can escape the apostrophe with a backslash (\). This tells the Python interpreter to ignore the special character following it. In this example, we use It\'s:

1
2
>>> 'It\'s a wonderful day in the neighborhood.'
"It's a wonderful day in the neighborhood."

You can also include numbers inside your strings:

1
2
3
>>> box_message = "I have 3 boxes."
>>> box_message
'I have 3 boxes.'

You can’t, however, add a number to a string. First you must convert the number to a string using the str function.

1
2
3
4
5
6
>>> '1' + '1'  # Adding strings
'11'
>>> 1 + 1  # Adding numbers
2
>>> 'hello' + str(1)  # Adding numbers to strings
'hello1'

See? Just as fun as kittens!

2 Comments

  1. Evan

    If we need to use an apostrophe in the middle of our sentence, we can either use the double quotation marks:

    >>> “It’s a wonderful day in the neighborhood.”
    “It’s a wonderful day in the neighborhood.”
    or we can escape the apostrophe with a backslash (\). This tells the Python interpreter to ignore the following special character:

    >>> ‘It\’s a wonderful day in the neighborhood.’
    “It’s a wonderful day in the neighborhood.”

    This part was confusing to me. In the first example, are you using the double quote? (Shift+”) or two single quotation marks back to back? Does it matter? And in the second part, why specifically does adding the backslash before the quotation mark in (It’s) produce double quotes in the return?

    Reply
    1. TorelTwiddler

      I see what you mean, and will edit the post to be more clear. Here’s the gist of it though:

      Double quotes mean " (Shift+') and an apostrophe is the same as a single quote ('). Two single quotes do not produce the same result as a double quote, and will often give you a syntax error (badly formatted code).

      As for the apostrophe in the sentence producing double quotes, the Python interpreter is smart, and rather than giving the string with the backslash in it (It\'s), instead it makes it easier to read by exchanging the single quotes for double quotes around the string (which is okay, because single and double quotes do the same thing!).

      Reply

Leave a Reply