Strings in Python

How to Use Strings in Python

In Python, you can assign a string to a variable using either double quotes or single quotes.

name="Andrea"
surname='Minini'

This flexibility is very useful because it allows you to include single (') and double quotes (") as regular characters without needing to escape them with a backslash (\).

What is a string? A string is an object composed of alphanumeric characters (letters, numbers, spaces, special characters, etc.).

How to Insert Special Characters in Strings

Some characters and symbols are considered "special" because they serve additional functions beyond being just characters.

An example of a special character. Double quotes " are characters but also function as string delimiters, making them special characters.

To insert a special character into a string, add a backslash \ before the character.

A Practical Example

Suppose you want to assign a string containing double quotes as characters within the string.

sentence="The \"backslash\" is the downward slash"
print(sentence)

The program's output is:

The \"backslash\" is the downward slash

If you don't use backslashes, the Python interpreter will treat the inner quotes as string delimiters, causing an error.

Similarly, you can handle other special characters as normal characters, such as the apostrophe (\').

surname='Dell\'anno'
print(surname)

You can also use certain letters as special characters to introduce functionalities within the string.

\n Line Feed (new line)
\r Carriage Return (move cursor to the beginning of the line)
\b Backspace (move back one character)
\t Tab (tab character)
\x Insert a character using the hexadecimal value of its ASCII code (e.g., \x21 prints the exclamation mark !)

Example

This string goes to a new line in the middle of the text.

string='this string goes to a new line \n in the middle of the text'
print(string)

The program's output is:

this string goes to a new line
in the middle of the text.

How to Handle Apostrophes and Quotes in Strings

Python allows you to handle apostrophes (') and quotes (") without using backslashes.

You can assign a string with double quotes using apostrophes as regular characters, and vice versa.

A Practical Example

When you use double quotes to assign a string, the apostrophe within the string is automatically treated as a regular character.

surname="Dell'anno"
print(surname)

The double quotes surrounding the string are not printed, while the apostrophe (') is displayed.

The program's output is:

Dell'anno

Conversely, when you use apostrophes to assign a string to a variable, the quotes within are considered special characters.

sentence='the language is called "Python" and it is very easy'
print(sentence)

The script's output is:

the language is called "Python" and it is very easy

In this case, the single quotes (') surrounding the string are not printed, while the double quotes (") are displayed on the screen as regular characters.

Assigning Strings with Triple Quotes

To assign an alphanumeric value without worrying about apostrophes or quotes in the text, you can use triple quotes.

In this case, the delimiters are three quotes at the beginning and end of the string.

string='''alphanumeric information'''

The text can include apostrophes, single quotes, and double quotes. They are automatically treated as simple characters.

Additionally, a text written with triple quotes automatically converts line breaks into \n.

Example

This string contains a mix of everything.

text='''this is an example with ' and " inside
without causing an error in Python'''
print(text)

The program's output is:

this is an example with ' and " inside
without causing an error in Python

Difference Between Strings and Lists

Strings should not be treated like lists in Python because, despite some similarities, there are many differences.

Like a list, a string is an ordered collection of elements.

A Practical Example

In this example, I first assign a string to the variable name.

In the second line, I use square brackets to display the fourth character of the string.

name="Andrea"
print(name[3])

The program's output is:

r

In this example, I treated the string like a list.

However, this is not always the case.

What Distinguishes a String from a List?

While a list allows me to modify each individual element [n], a string does not.

For example, you cannot change only the fourth position of the string.

name[3]="t"

This operation will cause an error. It is not allowed.

To modify an internal position of the string, you must reassign a value to the entire variable.

name="any other value"

To insert a character within a string, you need to concatenate the two parts with the string to be inserted.

Example

name='an-rea'
s = name[:2] + 'd' + name[3:]
print(s)

The script's output is:

Andrea

 
 

Please feel free to point out any errors or typos, or share suggestions to improve these notes. English isn't my first language, so if you notice any mistakes, let me know, and I'll be sure to fix them.

FacebookTwitterLinkedinLinkedin
knowledge base

Strings in Python

FAQ