
A string is a sequence of characters. The Python string data type is used to represent these sequences of characters (text). Python strings can contain letters (alphabet), numbers, and symbols. Python Strings are enclosed in single ('...'
), double ("..."
) or triple ("""...."""
, ‘''...'''
) quotations.
Table of Contents
Create String Variable and Assign Value
We assign a string or set of text to a variable in Python, by simply writing the name of the variable, and then using an equal sign (=
), and finally, put the string value.
The string value in Python can be defended using any of the following options:
Singleline String Value
The Singleline String Values are defined by enclosing the string values in either a double quote ("..."
) or a single quote ('...'
).
Example:
Output:
Multiline String Value
The Multiline String Values are defined by enclosing the string values in either triple-double quotes ("""..."""
) or triple-single quotes ('''...'''
).
Multiline Strings Values can span across multiple lines and can contain line breaks (new lines) and other special characters. n Python, this means that when line breaks or new lines are used in a multiline string value, the string will appear in the same position as they are in the code.
Example:
Output:
Access String Items
Accessing string items or characters can be done using indexing and slicing. Therefore, in Python, we can use square brackets [] to indicate the index number of the character(s) we want to access.
In the following subsections, we provide practical examples of how to access string items in Python using String Indexing (both positive and negative), and String Looping techniques.
String Indexing
String Indexing is a technique used to access individual characters at a specific position in the string, This can be done by using Positive Indexing and Negative Indexing.

Positive Indexing: This is a technique used when accessing items in a sequence (such as strings, lists, or tuples) by using a non-negative numerical index that starts from 0 and goes up to the length of the string items minus 1.
This means, positive indexing only allows us to access the string items from left to right. While the negative numerical index starts from -1 which from right to left and goes up to the first string. This one does not apply -1 to count the length since it starts -1.
Example:
Output:
Negative Indexing: This is a technique used when accessing items in a sequence such as strings, by using a negative numerical index that starts from -1 and goes up to the length of the string items.
This type of indexing mainly starts from the end of the string instead of the beginning of the string.
This means, negative indexing only allows us to access the string characters from right to left.
Example:
Output:
String Looping
String looping is a way to access string characters. In this case, we can use for
loop to access all the characters contained in a string.
The looping operation allows us to access one character from the string at a time and the looping process is continued until all the characters in the string are accessed.
Example:
Output:
An alternative way of using the for loop is to use the range()
function and pass the length of the string as an argument. The len()
function returns the length of the string.
Example:
Output:
Perform Operations on Strings
In Python, we perform operations on strings, such as combining two or more strings (concatenation) and also comparing strings using comparison operators.
We discuss how to combine and compare strings in the following subsections.
Combine Strings
Combine Strings: This combines two or more strings, by using the concatenation operator +
or the join()
method.
Combine string using concatenation operator +
.
Example:
Output:
Note: If you convert string numbers to other numeric types, the concatenation operator +
will add the numbers instead of combining them because they are no longer strings.
Example:
Output:
Combine Strings: This combines two or more strings, using the join()
method.
Combine string using the join()
method.
Example:
Output:
Compare Strings
We compare Strings using string comparison operators such as the double equal ‘==’ and other comparison operators such as greater than '>'
, greater than equal '>='
, less than '<'
, less than or equal '<='
, and not equal '!='
.
Note: In Python, string comparison is case-sensitive, which means, Java and java are two different words.
Example:
Output:
The above example only considers the equality comparison '=='
while in the following we consider other comparison operators.
Example:
Output:
String Length
String Length is referred to the number of characters (including spaces, punctuations, and special characters) it contains. The len()
function is used to determine the length of a string in Python.
Example:
Output:
Check String
In Python, we can check if a certain phrase or character is present in a string using in
keyword. The in keyword returns True
if the search character or phrase is present in the main string, otherwise it will return False
.
Other keywords used also include the not in
to check if a certain phrase or character is not present in a string.
Example:
Output:
Use NOT IN Keyword to Check String
To check if a certain phrase or character is not present in a string, we use the not in
. This will return True
if the characters or the desired word is not present otherwise returns False
as a result.
Example:
Output:
Use IF and IN Keywords to Check String
We can also use the if
and in
keywords together to check if a certain phrase or character is present in a string. This will lead us either to execute a block of code if such a decision is True
or skip a certain block of code to execute if it becomes False
.
Example:
Output:
Use IF and NOT IN Keywords to Check String
Similar to the above, we can also use the if
and not in
keywords together to check if a certain phrase or character is not present in a string. This will lead us either to execute or not to execute a block of code depending on the outcome (True
or False
)
Example:
Output:
Conclusion
In this article, we have explained and demonstrated the Python String. More specifically, we have discussed and practically illustrated several techniques related to how to create and assign value to a Python String, we also showed how to access string items using their index, and how to perform loops in String characters.
Additionally, we also illustrated and explained how to perform various operations on strings such as combining or concatenating Strings, comparing Strings, and last but not least we also showed how to use the len() function to check the length of the string and how to check if certain phrases or characters are present in a string.
All of the above will help you deeply understand the Python String data type and enhance your knowledge of Python. We also recommend learning other data types including the Python numeric data types and general data type practices in Python and other related resources.