- 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: A Comprehsnive Python Tutorial for Beginners and Professionals

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: Python Career Path - How & Why to Pursue Python Career Options!

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: What is Python IDE? Five IDEs for Python Developers you must Know

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!

fbicons FaceBook twitterTwitter google+Google+ lingedinLinkedIn pinterest Pinterest emailEmail


    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.

  • fb-15
  • twitter-15
  • linkedin-15


Trending Courses

AWS Course


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

Upcoming Class

2 days 04 Feb 2023

DevOps Course


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

Upcoming Class

1 day 03 Feb 2023

Data Science Course

Data Science

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

Upcoming Class

1 day 03 Feb 2023

Hadoop Course


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

Upcoming Class

1 day 03 Feb 2023

Salesforce Course


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

Upcoming Class

1 day 03 Feb 2023

QA Course


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

Upcoming Class

1 day 03 Feb 2023

Business Analyst  Course

Business Analyst

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

Upcoming Class

1 day 03 Feb 2023

MS SQL Server Course

MS SQL Server

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

Upcoming Class

1 day 03 Feb 2023

Python Course


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

Upcoming Class

15 days 17 Feb 2023

Artificial Intelligence  Course

Artificial Intelligence

  • Components of AI
  • Categories of Machine Learning
  • Recurrent Neural Networks
  • Recurrent Neural Networks
Artificial Intelligence  Course

Upcoming Class

1 day 03 Feb 2023

Machine Learning Course

Machine Learning

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

Upcoming Class

22 days 24 Feb 2023

Tableau Course


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

Upcoming Class

1 day 03 Feb 2023

Search Posts


Receive Latest Materials and Offers on Python Course