Day 9: Strings in Python


After few days working on the numbers and trying out different things using numbers, today let move into learning strings in Python. As I shared before, the strings can use single quote (‘…’) or double quote (“…”) in my previous blog. Also, I shared how to combine two string together using “+“.

Use “*” and “+”.
From the docs.python.org website, I found an interesting example of using “*”. Strings can be concatenated (glued together) with the + operator, and repeated with *. Example,
3 * ‘un’ + ‘ium’

It returns,
‘unununium’

Next, I want to share some escape syntax used in strings.

Escape Syntax

Next, I want to share some escape syntax used in strings.

Use “\” to escape single quote.
Example: print(‘I haven\’t eaten my lunch.’)

The output on the screen is I haven’t eaten my lunch. Alternatively, it suggests we can use double quote in our code, example, print(“I haven’t eaten my lunch.”) which gives the result.

Use “\n” to write newline using print statement.
Example:
s = ‘First line.\nSecond line.’
print(s)

The output on the screen:
First line.
Second line.

However, if I did not use print statement to display the output, it shows the output as,
First line.\nSecond line. It treats it as a string without any formatting.

Use “r” to use raw string before the first quote.
What does it mean? What if your print statement as below:
print(‘C:\some\name’)

The output on the screen:
C:\some
ame

It is not what we want to see especially when we want to print a file path. What we can do is put “r” before the quote,
print(r’C:\some\name’)
The output on the screen:
C:\some\name

What does %s, %r and %d do?

They are formatters that tell Python to take the variable on the right and put it in to replace the %s, for example, with its value. We can use %r for debugging since it displays the “raw” data of the variables, but we use %s and other formatters for displaying in the screen.

Strings are ordered sequences and it means it can be indexed and sliced to get subsection of the string.

If you are familiar with substring, this is what it meant.
More example on index and slicing will share in the next blog.

Let me move on to check the attributes and properties in strings. For both online text editors and IntelliJ IDE, I can access the list of all the attributes and properties when I place a dot “.” after the variable. Example as below:

After declare the variable, 
x = ‘Hello World’
I can see the long list of attributes and properties from the drop down list when I type x[dot]. The dot allows me to access the properties list. The most common ones are, upper(), lower(). split()

Summary of the day:

  • string concatenation and multiplication.
  • string escape syntax.
  • formatters.
  • index of a string.
  • slicing of a string.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s