1.2. Strings

A string is a series of characters. Anything inside quotes is considered as a string. You can use single or double quotes for strings like:

"This is a string"
`This is also a string`

This two options allows you to use quotes and apostrophes in your string.

`He says, "Hello World!"'
"It's fun to learn programming."

Lower & upper case using method

A method is an action that Python can do on a piece of data. Look at the following example.

In this example, the variable name refers to monty python, all in lowercase. The dot (.) after name in name.title() tells Python to make the title() method act on the variable name.

> You should see this output.Monty Python
> You can change a string to all uppercase by using .upper() and to all lowercase using .lower(). Try this out:name = "monty python"print(name.upper())print(name.lower())

Variables inside strings

Sometimes you want to use variable inside a string, like to print your name in "Hello my name is ...". Let's see an example.

To insert a variable's value into a string, put the letter f right before the opening quotation mark (see3rd line). Also, put braces { } around the variable you want to use.

> These strings are called f-strings.

Whitespaces: tabs & newlines

To add a tab to your text, use the character combination \t. To add a newline in a string, use the character combination \n.

Stripping whitespace

Extra whitespaces can cause confusion to programmers. See the following two variables:

string1 = "python"

string2 = "python "

There is an extra space at string2. We can remove this by using strip() method.

You can strip the whitespace on the right of the string by using rstrip(), and on the left of string using lstrip().

Exercise 1.2

1. Personal Messages
Use a variable to represent your name and write a message like the following:
"Hello, my name is [your_name]."

2. Name Cases
Use a variable to represent your full name and then print that in lowercase, uppercase, and title case.

3. Famous Quote
Try to print the following sentence in Python. Print each line separately.

"A person who never made a mistake
never tried anything new."
- Albert Einstein

4. Famous Quote 2
Try to print the text in no.3 again but this time, wake a new variable called famous_person and replace Albert Einstein using that.

5. Stripping The Whitespaces
In the code, use each of the three stripping functions, lstrip(), rstrip(), and strip() to get rid the whitespaces.

6. Escape Characters
Some of the characters cannot appear in print. Search in the web a way to print the following sentence:
He said, "I'm so f#@%ed!"