
This tutorial is meant to provide a list of examples that will help you to practice Python Strings and fully understand some of the basics of Python Strings easily. The following subsections contain simple and easy-to-understand Python code dedicated to strings. Learn more about Python examples using this simple cheat sheet.
Table of Contents
Store String Values in a Variable
Example 1: Store string values in a Python variable.
Output:
Store Singleline String Value in a Variable
Example 2: Similar to the above we store string values in a Python variable, this is also called single line string value and we cannot insert new lines.
Output:
Try to insert a new line in the above string value and see what happens.
Example 3: Insert a new line in a single line string value.
Output:
Python triggers an error if you try to insert a new line in a single line string value.
Store Multiline String Value in a Variable
Example 4: Store Multiline string values in a Python variable. Use triple quotes, single or double.
Output:
Check String Data Type
Example 5: Use the type()
function to check string type in Python.
Output:
Check String Length
Example 6: Use len()
function to check the total number of characters contains in a string.
Output:
String Slicing
Example 7: Index and select the first, second, third, and last character of the string.
Output:
Example 8: Index and select the first half characters of the string.
Output:
Example 9: Index and select the last half characters of the string.
Output:
Example 10: Index and select the strings ranging from the first 20% to the last 70% of characters of the string.
Output:
Example 11: Slicing from the start (do not specify the start position/index, by default is 0) and slicing to the end (do not specify the last position/index, by default is the last index).
Output:
Negative indexing: We use negative numbers to index, this indexing starts from the end of the sequence and counts backward.
Example 12: Using a negative number at the beginning of the slicing means slicing will start from the end of the string.
Output:
Example 13: Using negative numbers display the last three characters of the string using string slicing in Python.
Output:
Example 14: Using string slicing in Python display all the string characters except for the last character.
Output:
Example 15: Display the last 50 percent of the string characters using negative indexing or slicing in Python.
Output:
Combine String and Numbers
In Python, you cannot combine or concatenate string values into numbers (int and float) or boolean (bool) using the additional operator +
. If you do so, it will result in an error.
Example 16: Combining string and int using +
operator.
Output:
Example 17: Combining string and float using +
operator.
Output:
Example 18: Combining string and boolean (bool) using +
operator.
Output:
Example 16 to Example 18 has failed due to illegal concatenation. To correct the above code we can use a new Python feature called String Formatting by using f-Strings or format() method.
String Formatting
String formatting refers to the process of creating a string that contains all kinds of data values in a specified format. For example, you want to concatenate a sequence of string characters into numerical data without using Python’s string concatenation sign '+'
.
There are several options we can adopt to format our string and combine several data types in it using either f-Strings or format() method.
F-Strings Formatting
F-Strings are string formatting techniques used in Python to provide us a convenient way of combining numbers, and other expressions inside string literals. The f-strings feature are new in Python and only be found in Python version 3.6.
F-Strings make our code more readable and ease to understand.
We use F-strings by placing an “f” or “F” character in front of a string literal.
Example 19: Combining string, and numbers such as int, float, and boolean (bool) using the f-string formatting technique.
Output:
String Formatting using format() Method
String formatting is one of the correct ways of combining string and numbers such as int, float, boolean and etc. In this case, we can use format()
method.
To use the format() method correctly you have to insert placeholders inside the string. The placeholders are specified using the using curly brackets {}
.
Example 20: Combining string, and numbers such as int, float, and boolean (bool) using format()
method.
Output:
The above example uses an empty placeholder while the following examples use named and numerical placeholders when using format()
method.
Example 21: Using format()
method with named placeholders.
Output:
Example 22: Using format()
method with numerical placeholders.
Output:
Conclusion
This Python tutorial article is dedicated to providing several ways to practice Python Strings including assigning string values to a variable, checking its lengths and type, string slicing, string formatting, and many more. The aim of this tutorial is to provide simplified String Python examples. Further practices of string such as string basics and string methods can also be practiced.