top of page

Strings in Python

Strings are a fundamental data type in Python and are used to represent textual data. In this article, we will discuss everything you need to know about strings in Python, including how to create them, manipulate them, and perform operations on them.

Creating Strings in Python

In Python, you can create strings by enclosing text in single quotes, double quotes, or triple quotes. Here are some examples:

The string1 and string2 variables contain single-line strings, while string3 is a multi-line string that is created using triple quotes. You can use any of the three types of quotes to create a string, but the choice depends on the requirements of your string.

Accessing Characters in a String


You can access individual characters in a string using their indices. The indices of a string start at 0 and go up to the length of the string minus one. Here is an example:

In the above example, we are accessing the first and eighth characters of the string using their indices.


Manipulating Strings in Python


Python provides several built-in functions for manipulating strings. Here are some of the most commonly used ones:

In the above examples, we are using the + operator to concatenate two strings, finding the length of a string using the len() function, extracting a substring using slice notation, converting a string to uppercase or lowercase, and replacing a substring in a string using the replace() function.

String Formatting in Python


String formatting is a technique used to create dynamic strings that can change based on the values of variables. Python provides several ways to format strings, including the % operator, the .format() method, and f-strings. Here are some examples:

In the above examples, we are using the % operator, the .format() method, and f-strings to create dynamic strings.


In this article, we discussed everything you need to know about strings in Python. We covered how to create strings, manipulate them, access individual characters, and perform string formatting.

bottom of page