- Python Blogs -

Learn Python Strings : An Expert Guide

Strings in Python are created using single quotes (‘ ’) or double quotes (” ”) or Triple quotes (“”” “””). It is represented by python built-in String class named as “str”.

Everything is Python is considered as an object and Strings in python as objects as well. Python provides a number of built-in functions or methods to modify them

For example:

myNameDouble = “Janbask”
myNameSingle = ‘Janbask’

All three will be considered as same and assign to respective variable names as follows: 

Learn Python String

myNameDouble, myNameSingle and myNameTriple will have the same datatype as “str” built-in class as follows:

Learn Python String 2

With Triple Quotes Strings can extend to Multiple lines as follows:


>>> myNameMulti="""janbask has good online ... tutorials for Python Learning"" 
>>> myNameMulti 'janbask_has good online \ntutorials for Python Learning'

String Concatenation

In Python, we can concatenate or combine strings using “+” operator and “*” operator. The new string that is created is referred to as a string object.

Joining of two or more strings into a single string is called string concatenation


>>> myStr1 = “JanBask”
>>> myStr2 = “Python tutorials”
>>> myStr3 = myStr1 + myStr2

“myStr3” will be the final new string is created as a result in string object form

Learn Python String

Now as you can see in the above output there is no space in “JanBask” and “Python” words

We can use “ ” with space to concatenate strings with space as follows:


>>> myStr3 = myStr1 +" "+ myStr2 
>>> myStr3 l' JanBask Python tutorials' 

We can concatenate two or more strings as well as follows:


>>> mystr1 = "janbask" 
>>> mystr2 = "python" 
>>> mystr3 = "tutorials" 
>>> mystr4 = mystrl + " "+ mystr2 + " "+ mystr3 
>>> mystr4 janbask python tutorials'

Using “*” operator, it repeats the given string as per the mentioned number of times.

>> mystr1="Janbask online tutorials" 
>>> mystr1*3 "Janbask online tutorialsJanbask online tutorialsJanbask online tutorials" 

Read: An Introduction to Time Series Analysis with Python

Why are Python strings immutable?

mutable object can be changed after its creation, and an immutable object can not be changed once it is created

Strings (str) are immutable objects.

Now to understand that object is immutable or mutable. We need to understand “id()” and “type()” functions.


This Identity function is used to return the unique identity for a specified object. This id is assigned to the object at the time of its creation only.

The id of an object is the unique memory address of the object. It returns an integer value which is unique and always remains constant until its lifetime expires.


>>> myStr3 'JanBask Python tutorials' 
>>> id (myStr3) 1363574353472 
>>> myStr2 = "Python tutorials" 
>>> id (myStr2) 1363574547776

“is” operator

This operator is used to compare the types of the two objects created-


“==” operator

>>> mystr1="janbask"
>>> mystr2="janbask" 
>>> id(mystr1) 1363574668176 
>>> id(mystr2) 1363574668176 
>>> print (mystri is mystr2) True


It returns the type of a given variable that is passed as an argument in during runtime.


>>> type (mystr1) 
<class 'str'>
 >>> type(mystr2) kclass 'str'> 
>>> x=30 
>>> type(x) 
<class 'int'> 

Examples to demonstrate immutable vs mutable 

We have created “mystr1” variable object and “mystr2” variable object. Both points to same object.

But after updating “mystr1”. The object tagged with “mystr1” got changed and object tagged with “mystr2” remains the same.


>>> mystr1="janbask" 
>>> mystr1=my str2 
>>> id(mystr1)==id(mystr2)



>>> mystr1=mystr1+" python" 
>>> mystri 'janbask python'
>>> id(mystr1)==id(mystr2) 


This proves that Immutable object does not allow modification once it is created and “str” objects are immutable.

Read: Top 10 Python Libraries For Machine Learning

String Slicing


Mystr1[start_pos : end_pos]

The String slicing starts with “start_pos” (included in output) and ends with “end_pos” (excluded from output). Only “end_pos-1” position is included in the final output.

For example:

In the above example:

mystr4[0:7] -> outputs items from 0th position to 6th position from “mystr4”

mystr4[0:8] -> outputs items from 0th position to 7th position from “mystr4”

mystr4[7:14] -> outputs items from 7th position to 13th position

mystr4[9:18] -> outputs items from 9th position to 18th position from “mystr4”

Reverse of a string using Slicing

We can totally reverse all the letters in the string by providing step value in the slicing syntax as follows:


mystr1[start_pos : end_pos : Step_value]



>>> mystr4 = "Janbask" 
>>> mystr4[::-1] 'ksabna)'

Deletion of a string

An entire string is deleted with the use of “del” keyword. After deletion, if we try to print or access this string variable, we get an error.


Length of a string

len() is used to returns the number of characters in the string.


>>> mystr1="Janbask online tutorials"
>>> len(mystr1)

Read: Upgrade Your Programming Career by Learning Python & Getting Certified

Get the index for substring

Index() is used to return the index of substring or a single character in a string


>>> mystr1="Janbask online tutorials"
>>> mystr1.index("o")
>>> mystr1.index("tutorials") 

Traversing the string using for loop

It will print one character at a time.


Verifying the “empty” string

A string can be checked that it is empty or not. The Boolean of empty string returns false and Boolean of non- empty string returns True.


>>> mystr1="Janbask online tutorials" 
>>> print(bool(mystr1)) 



>> mystr2="" 
>>> print(bool(mystr2)) 


String formatting operators

These operators allow you to embed variables/values inside a string or substring.

Embedding String in a string using %s


>>> mystr1="Janbask online tutorials"
>>> print("the best portal is - %s" %my str1) 
the best portal is - Janbask online tutorials

Embedding character in string using %c


>>> mystr2="V"
>> print("the best portal is - %c" %mystr2), 
the best portal is - V


With this write-up, we have understood the String functions available in Python programming language. The above examples are of basic level and hence, can be understood easily by a novice in Python language. Happy coding!

FaceBook Twitter Google+ LinkedIn Pinterest Email

    Janbask Training

    A dynamic, highly professional, and a global online training course provider committed to propelling the next generation of technology learners with a whole new way of training experience.


Trending Courses


  • AWS & Fundamentals of Linux
  • Amazon Simple Storage Service
  • Elastic Compute Cloud
  • Databases Overview & Amazon Route 53

Upcoming Class

4 days 30 Sep 2022


  • Intro to DevOps
  • GIT and Maven
  • Jenkins & Ansible
  • Docker and Cloud Computing

Upcoming Class

11 days 07 Oct 2022

Data Science

  • Data Science Introduction
  • Hadoop and Spark Overview
  • Python & Intro to R Programming
  • Machine Learning

Upcoming Class

4 days 30 Sep 2022


  • Architecture, HDFS & MapReduce
  • Unix Shell & Apache Pig Installation
  • HIVE Installation & User-Defined Functions
  • SQOOP & Hbase Installation

Upcoming Class

4 days 30 Sep 2022


  • Salesforce Configuration Introduction
  • Security & Automation Process
  • Sales & Service Cloud
  • Apex Programming, SOQL & SOSL

Upcoming Class

4 days 30 Sep 2022


  • Introduction and Software Testing
  • Software Test Life Cycle
  • Automation Testing and API Testing
  • Selenium framework development using Testing

Upcoming Class

4 days 30 Sep 2022

Business Analyst

  • BA & Stakeholders Overview
  • BPMN, Requirement Elicitation
  • BA Tools & Design Documents
  • Enterprise Analysis, Agile & Scrum

Upcoming Class

4 days 30 Sep 2022

MS SQL Server

  • Introduction & Database Query
  • Programming, Indexes & System Functions
  • SSIS Package Development Procedures
  • SSRS Report Design

Upcoming Class

4 days 30 Sep 2022


  • Features of Python
  • Python Editors and IDEs
  • Data types and Variables
  • Python File Operation

Upcoming Class

18 days 14 Oct 2022

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks

Upcoming Class

12 days 08 Oct 2022

Machine Learning

  • Introduction to Machine Learning & Python
  • Machine Learning: Supervised Learning
  • Machine Learning: Unsupervised Learning

Upcoming Class

25 days 21 Oct 2022


  • Introduction to Tableau Desktop
  • Data Transformation Methods
  • Configuring tableau server
  • Integration with R & Hadoop

Upcoming Class

4 days 30 Sep 2022

Search Posts


Receive Latest Materials and Offers on Python Course