Tutorial Playlist

Python Tutorial for Beginners

Overview

How to Install Python on Windows?

Lesson - 1

Python OOPs Concept: Here's What You Need to Know

Lesson - 2

Getting Started With Jupyter Network

Lesson - 3

PyCharm Tutorial: Getting Started with PyCharm

Lesson - 4

A Beginner’s Guide To Python Variables

Lesson - 5

Python Numbers: Integers, Floats, Complex Numbers

Lesson - 6

Learn A to Z About Python Functions

Lesson - 7

The Basics of Python Loops

Lesson - 8

Introduction to Python While Loop

Lesson - 9

Python For Loops Explained With Examples

Lesson - 10

Understanding Python If-Else Statement

Lesson - 11

Introduction to Python Strings

Lesson - 12

Everything You Need to Know About Python Slicing

Lesson - 13

All You Need To Know About Python List

Lesson - 14

Python Regular Expression (RegEX)

Lesson - 15

An Introduction to Python Threading

Lesson - 16

Objects and Classes in Python: Create, Modify and Delete

Lesson - 17

The Best Python Pandas Tutorial

Lesson - 18

A Beginner's Guide To Web Scraping With Python

Lesson - 19

A Handy Guide to Python Tuples

Lesson - 20

How to Easily Implement Python Sets and Dictionaries

Lesson - 21

Everything You Need to Know About Python Arrays

Lesson - 22

An Introduction to Matplotlib for Beginners

Lesson - 23

An Introduction to Scikit-Learn: Machine Learning in Python

Lesson - 24

Top 10 Python IDEs in 2020: Choosing The Best One

Lesson - 25

The Best NumPy Tutorial for Beginners

Lesson - 26

Python Django Tutorial: The Best Guide on Django Framework

Lesson - 27

Top 10 Reason Why You Should Learn Python

Lesson - 28

How To Become a Python Developer

Lesson - 29

Top 50 Python Interview Questions and Answers for 2020

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