Introduction to Python Strings

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”.

Python Training Course

Learn Data Operations in PythonExplore Course
Python Training Course

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’’’).

Example:

example

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.

accessing

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. 

slicing

Fig: Slicing of a string

3. String length

To find the length of a string, use the len() built-in function.

Example:

string-length

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.

Example:

concetanation

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’)

Example:

splitting.

Fig: Splitting a string

The str() constructor

Python has a built-in function str(), which returns a printable string representation of any object.

Example:

/convert

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. 

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:

backslash

Fig: backslash sequence of Python string

Here are some useful escape sequences to use with strings:

Fig: Some useful escape sequences

String Methods

Python has a set of built-in methods that you can use on string objects. These are:

  • capitalize()

The capitalize() method converts the first character of the string in uppercase and puts all other characters into lowercase.

Syntax: string.capitalize()

Example:

capitalize

Fig: capitalize() method

  • strip()

The strip() method removes any whitespaces from the beginning and the end of a string.

Syntax: string.strip()

Example:

strip

Fig: strip() method

  • lower()

The lower() method returns the string in lower case.

Example:

lower.

Fig: lower() method

  • upper()

The upper() method returns the string in upper case.

Example:

upper

Fig: upper() method

  • replace()

The replace() method replaces one string with another.

Example:

replace

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 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.

About the Author

SimplilearnSimplilearn

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.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.