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.
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”.
String literals are written by enclosing a sequence of characters in a single quote (‘Hello’), double quotes (“Hello”), or triple quotes (‘’’Hello’’’).
Example:
Fig: Example of Python strings
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
To find the length of a string, use the len() built-in function.
Example:
Fig: To find the length of a string
The term “concatenation” means joining two strings together into a single line. We can concatenate two strings by using ( + ) operator.
Example:
Fig: Concatenation of Python strings
Python’s split() method splits the given string based on a specific separator.
Syntax: str.split( ‘separator’)
Example:
Fig: Splitting a string
Python has a built-in function str(), which returns a printable string representation of any object.
Example:
Fig: Convert any data type into Python string
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.
Example:
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.
Syntax: string.capitalize()
Example:
Fig: capitalize() method
The strip() method removes any whitespaces from the beginning and the end of a string.
Syntax: string.strip()
Example:
Fig: strip() method
The lower() method returns the string in lower case.
Example:
Fig: lower() method
The upper() method returns the string in upper case.
Example:
Fig: upper() method
The replace() method replaces one string with another.
Example:
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.
We have discussed Python Strings and its different operations and methods. Check out our video tutorial to learn more about Python strings. 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.
Name | Date | Place | |
---|---|---|---|
Full Stack Java Developer | Cohort starts on 10th Mar 2021, Weekend batch | Your City | View Details |
Full Stack Java Developer | Cohort starts on 22nd Mar 2021, Weekend batch | Chicago | View Details |
Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.
Full Stack Java Developer
Python Training
*Lifetime access to high-quality, self-paced e-learning content.
Explore Category