Python Strings

About Python Strings

In Python, a string is a sequence of characters.

It is is used to represent text.

In Python, there are single-line string and multi-line string.

Creating Single-line string

You can create a single-line string using single quotes (') or double quotes (").

Anything between ' and ' or " and " will be treated as string.

In the following example, 'Python is great!' assigned to the variable 'first_string' to create a single-line string using single quotes (').

"Python is awesome!" assigned to the variable 'another_string' to create a single-line string using double quotes (")

'print()' function is used to display the strings.

Please have a closer look at the following examples:

Example of single-line string:
Output of the above example
    Python is great!
    Python is awesome!
                                

Creating Multi-line string

You can create a multi-line string using three single quotes (''') or three double quotes (""").

Anything between ''' and ''' or """ and """ will be treated as multi-line string.

In the following example, two multi-line strings have been created using three single quotes (''') and three double quotes (""").

Please have a closer look at the following examples:

Example of multi-line string:
Output of the above example
    Python is the
    most popular 
    programming language.
    Python is an awesome
    programming language.
                                

String Operations

String Concatenation: Strings can be concatenated using the '+' operator.

Example of string concatenation:
Output of the above example
    This is an example of string concatenation
                                

String Slicing: A substring can be extracted from a string using slicing.

You need to define the starting and closing indices to get a substring from a string.

In the following example, my_string[0:6] is used to extract a substring from 'my_strin' string variable.

Here 0 is the starting index of the string from where the substring will start and 6 is the index before which the substring will end (character at index 6 will not be included in the substring).

Example of substring:
Output of the above example
    Python
                                

Accessing String Characters: Individual characters in a string can be accessed using indexing as string is an array. Python uses zero-based indexing.

Please note that as Python string is an array, you can loop through a string.

In the following example, my_new_string[0] is used to get the first character of the 'my_new_string' string variable.

Example of accessing string characters:
Output of the above example
    P
                                

Python Compiler For Live Practice

Practice the above Python code in the following Python Editor. Create more Python string variables and perform different string operations in the following Python code editor and click Execute button. Output will be displayed in Python Code Output section.

Python logo Python Editor
Python Code Output