Introduction to Strings

code guest
Python Journey
Published in
3 min readAug 7, 2019

One data type that you will be working a lot with is the string data type which represents text information.

And we encounter text information all the time on our digital devices — all the text you see are stored as strings.

Now, text is made up of characters like numbers, alphabets, symbols etc. Some examples of text include $99.99, ^_^, :) and so on.

We encounter digital text everywhere. These are all stored as strings. (credits: undraw.co)

Since text information are stored as strings, therefore a string is basically just a sequence of characters.

To identify data as strings within python, we enclose text with either single quotes (‘), double quotes (“), or triple quotes (‘‘‘ or ‘‘‘‘‘). Both single and double quotes have the same meaning in python so you can choose which one you’d like to use.

When you use single or double quotes to denote a string, the opening and closing quotes must appear on the same line in your program. Whereas using triple quotes let us denote multi-line strings.

Fun fact: The name string itself came from the idea of representing a sequence of characters.

Special characters

Since we know strings are enclosed by quotes, what happens if we have quotes within our string (eg. “Tommy the cat said “Hello world!” to me”)?

To let Python know that the quotes in “Hello World!” are part of the whole string, we need to introduce the special character backslash \.

Within a string, the backslash character which is \ tells Python to anticipate a special character sequence.

Here are some examples of special character sequences:

  • \a
  • \”
  • \’
  • \n

Note that all special character sequences start with a backslash.

Special character sequences are handled by Python in unique ways:

  • By using backslash in front of a quote like this \”, we are telling Python not to treat the quote as marking the end of a string but as part of the string.
  • By using \n (also called the new-line sequence), we can get Python to output a string in multiple lines instead of the whole string in one long line.
  • By using \a, we can get Python to get our computer to sound an alarm bell. Visit https://repl.it/repls/GigaPaleRegisters for a demo!

Therefore, the correct way to store the example sentence about Tommy the cat would be “The cat said \“Hello world!\” to me”.

Also, Python doesn’t print out the backslash because it knows that the backslash denotes a special character sequence. That is unless we tell Python specifically to print backslash but using the backslash backslash \\ sequence.

For a list of special sequences and more information on strings in Python, visit this awesome site by Mr Phil Spector from UC Berkeley!

External links

Summary

  • Strings are sequences of characters that represent text information
  • Strings are enclosed by single quotes, double quotes or triple quotes
  • Strings have special character sequences starting with backslash that are handled by Python in unique ways

Quiz

Which is not a string?

a) “a\a”

b) “Why\\ did the chicken cross the road?”

c) 123\

d) True

Quiz explanation

Strings must be enclosed by quotes.

We can find special character sequences within strings and these sequences must begin with a backslash.

Quiz answers: a & b

--

--