Python String

Python String is a sequence of characters. Since it is a sequence, we can use index to access individual characters.

Define String

To define a string literal, we can either surround the sequence of characters in single quotes or double quotes.

</>
Copy
#define string using single quotes
x = 'abcd'
#define string using double quotes
x = "abcd" 

When we use single quotes to define a string, and would like to have a single quote in the string, we must escape it. The same explanation holds for double quote as well.

</>
Copy
#escaping single quote character
x = 'abc\'de' 
#escaping double quote character
x = "abc\"de" 

When single quotes are used to define a string, we can have double quote character in the string without any escaping.

</>
Copy
#double quote in string
x = 'abc"de' 
#single quote in string
x = "abc'de" 

Multiline Strings

We can define a multiline string in the Python code using enclosing triple quotes """ """ or ''' '''.

In the following example, we use triple single-quotes to define a multiline string, and print it to console.

Python Program

</>
Copy
x = '''Menu
1. Apple
2. Banana
3. Cherry
4. Mango
'''

print(x)

Output

Menu
1. Apple
2. Banana
3. Cherry
4. Mango

In the following example, we use triple double-quotes to define a multiline string, and print it to console.

Python Program

</>
Copy
x = """Menu
1. Apple
2. Banana
3. Cherry
4. Mango
"""

print(x)

Output

Menu
1. Apple
2. Banana
3. Cherry
4. Mango

Raw Strings

By default, backslash character in a string is considered as a special character to define escape characters like new line, tab space, etc., or to include single/double quote characters.

We can tell Python Interpreter to treat backslash character just like any other character, and not use as an escape character. To do so, we have to define the string as a raw string, which can be done as shown in the following program.

Place the character r before the string literal. r stands for raw.

In the following example, we use triple single-quotes to define a multiline string, and print it to console.

Python Program

</>
Copy
x = r'The default path is "C:\Program Files\"'
print(x)

Output

The default path is "C:\Program Files\"

Variables inside a String

We can use variable names inside a string literal, and the Python Interpreter can replace the variable name with its value during runtime. For that, we have to use f-strings or formatted-strings.

Place the character f before the string literal. f stands for formatted.

In the following example, we use f-strings to use a variable name inside the string literal.

Python Program

</>
Copy
pi = 3.14
x = f'The value of PI is {pi}'
print(x)

Output

The value of PI is 3.14

Accessing Characters

Since strings are sequences, which means they are ordered. And therefore, we can use index to access individual characters in string.

Python Program

</>
Copy
x = 'abcdef'
print(x[0])
print(x[2])

Output

a
c

String Methods

Refer Python String Methods tutorial. This tutorial will tabulate all the methods of Strings in Python, with respective tutorials for each of the methods.

Conclusion

In this Python Tutorial, we learned what strings are, how to define a string is Python, how to escape characters, how to write multiline strings, how to use variables inside a string, and access individual characters of a string.