17
FebPROMO : GET UP TO 20% OFF ON LIVE CLASSES + 2 SELF-PACED COURSES FREE!! - SCHEDULE CALL
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”
or,
myNameSingle = ‘Janbask’
or
myNameTriple=”””Janbask”””
All three will be considered as same and assign to respective variable names as follows:
myNameDouble, myNameSingle and myNameTriple will have the same datatype as “str” built-in class as follows:
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
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?
A 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.
Id()
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
Type()
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)
True
>>> mystr1=mystr1+" python"
>>> mystri 'janbask python'
>>> id(mystr1)==id(mystr2)
False
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
Syntax
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:
Syntax
mystr1[start_pos : end_pos : Step_value]
Code
>>> 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)
24
>>>
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")
8
>>>
>>> mystr1.index("tutorials")
15
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))
True
>> mystr2=""
>>> print(bool(mystr2))
False
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
>>>
Summary
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!
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.
AWS
DevOps
Data Science
Hadoop
Salesforce
QA
Business Analyst
MS SQL Server
Python
Artificial Intelligence
Machine Learning
Tableau
Search Posts
Related Posts
Receive Latest Materials and Offers on Python Course
Interviews