Python is a high-level, interpreted programming language developed in the late 1980s that has enjoyed tremendous growth over the last few years. While it can be used for many different applications, its resurgence in popularity has been driven by the surge in data science and business intelligence. In 2018, Python was ranked as the most highly sought technology of the year by the Stack Overflow Developer Survey.
In this article, we will learn about one of Python programming's fundamental data types: Python strings, including an overview of how to work with them, Python string methods, and more. So, let’s get started.
What Is a Python string?
A “string” is a data type in Python, composed of a collection of characters. It can include letters, numbers, and even special characters. For example: “Simplilearn”, “Version 1.0”.
Working with Strings
1. How to create a string
String literals are written by enclosing a sequence of characters in a single quote (‘Hello’), double quotes (“Hello”), or triple quotes (‘’’Hello’’’).
Fig: Example of Python strings
2. Accessing characters by the index of a string
The string indexing starts from 0. We can access individual characters in a string by mentioning the index number in square brackets.
Fig: Accessing character of a string
We can also return a range of characters by using the slice syntax. When slicing, the starting index is included in the output, but the ending index is not included.
Fig: Slicing of a string
3. String length
To find the length of a string, use the len() built-in function.
Fig: To find the length of a string
4. String concatenation
The term “concatenation” means joining two strings together into a single line. We can concatenate two strings by using ( + ) operator.
Fig: Concatenation of Python strings
Splitting a String
Python’s split() method splits the given string based on a specific separator.
Syntax: str.split( ‘separator’)
Fig: Splitting a string
The str() constructor
Python has a built-in function str(), which returns a printable string representation of any object.
Fig: Convert any data type into Python string
Python Escape Sequence
If we want to print a text (for example: Mike told me, “Jack said, ‘don’t do it.'”), we cannot write it by using a single quote or double quote. It will show a syntax error.
Fig: Syntax error
We can use an escape sequence to avoid this problem. A series of characters has indirect meaning when placed inside a double quotation.
Fig: backslash sequence of Python string
Here are some useful escape sequences to use with strings:
Fig: Some useful escape sequences
Python has a set of built-in methods that you can use on string objects. These are:
The capitalize() method converts the first character of the string in uppercase and puts all other characters into lowercase.
Fig: capitalize() method
The strip() method removes any whitespaces from the beginning and the end of a string.
Fig: strip() method
The lower() method returns the string in lower case.
Fig: lower() method
The upper() method returns the string in upper case.
Fig: upper() method
The replace() method replaces one string with another.
Fig: replace() method
Learn data operations in Python, strings, conditional statements, error handling, and the commonly used Python web framework Django with the Python Training course.
Python Strings and Beyond
We have discussed Python Strings and their different operations and methods. Check out our video tutorial to learn more about Python strings. Also, check out our tutorial on Map in Python. Simplilearn offers several Python educational programs online that can help you get up to speed and career-ready in this exciting and sought-after technology, including our foundational Python Training Course.