String Data Type in Python

String

String is a collection of the character, it is ordered for unicode characters denoted by the single or double inverted comma, String are used for the store text information to any variable, String are immutable it cannot be changed with it.

Mutable vs Immutable Object

Mutable objects are objects which can be modified in memory after creation. Immutable objects are objects which cannot be modified in the memory area.

  • Immutable Objects: int, float, long, complex, string, tuple, bool
  • Mutable Objects: list, dict, set, byte array, user-defined classes

String Visualize

String visualization is a similar as the List visualization now we will see some demonstration of view string ,

String Indexing:

This is indexing of the given demonstration along with the syntax.

Syntax: object[index]

S A L E S F O R C E D R I L L E R S
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
-18 -17 -16 -15 -14 -13 -12 -11 -10 -9 -8 -7 -6 -5 -4 -3 –2 -1

View Particular String

Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> str="Salesforcedrillers"
>>> str[1]
'a'
>>> str[2]
'l'
>>> str[12]
'i'
>>> str[13]
'l'
>>> str[-1]
's'
>>> str[-10]
'c'
>>> str[-17]
'a'

String Slice:

Slicing is used to see the customized data here we give the demonstration along with syntax.

Syntax: object[start : stop : stride(step)]

start (optional) starting index value, the default is 0 (in fwd indexing)
stop (optional) ending index value, is not included in the generated sequence, default is len(object)(in fwd indexing)
step (optional) the difference between each item in the sequence, the default is 1(in fwd indexing) and -1 (in backward indexing)
Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> str="Salesforcedrillers"
>>> str[-3:]
'ers'
>>> str[-2:]
'rs'
>>> str[:3]
'Sal'
>>> str[:4]
'Sale'
>>> str[-1:4]
''
>>> str[1:7:2]
'aef'
>>> str[-1:-17:-2]
'selrerfe'
>>>

String Insertion:

Insertion of string refers to the creation of String, we can use single quotes or double quotes and triple quotes for creating the string

Let’s take the example of string creation,

Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> str='using single quotes for creating string'
>>> print(str)
using single quotes for creating string
>>> str1="using double quotes for creating string"
>>> print(str1)
using double quotes for creating string
>>> str2=''' now I am using triple,single quotes
... for creating string
... '''
>>> print(str2)
 now I am using triple,single quotes
for creating string

>>> str3="""Now I am using triple,double quotes
... for
... create
... string
... """
>>> print(str3)
Now I am using triple,double quotes
for
create
string

>>>

Run time input

In this tutorial we will take the input from user,

Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> str=input("Enter Anything")
Enter Anything I am using User input method
>>> print(str)
 I am using User input method
>>> a=input("Name :- ")
Name :- Ankita
>>> print(a)
Ankita
>>> type(a)

>>> type(str)

>>>

String Formatting

String formatting is done to add formatted objects to string.we can perform string formatting in three ways:

% String formatting operator.
format() It is a built-in function. Returns a formatted representation of the given value controlled by a given format specifier.
f-string This method can only be applied to Python 3.5+ versions. Prefix the string with f and put your object inside {}.
Python 3.8.4 (tags/v3.8.2:7b3ggab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
>>> name = "Einstein"
>>> age = 22
>>> percent= 99.9
>>> full = "My name is %s.\nMy age is %d.\nI secured %.2f%%." %(name, age, percent)
>>> print(full)
My name is Einstein.
My age is 22.
I secured 99.90%.
>>> full = "My name is {}.\nMy age is {}.\nI secured {:.2f}%.".format(name, age, percent)
>>> print(full)
My name is Einstein.
My age is 22.
I secured 99.90%.
>>> full = f"My name is {name}.\nMy age is {age}.\nI secured {percent:.2f}%."
>>> print(full)
My name is Einstein.
My age is 22.
I secured 99.90%.

Deletion/Updation of String

Deletion is basically used for erasing some data from a given string or data,and updation is used for replacing some information from given data.

Deletion

Python 3.8.4 (tags/v3.7.8:4b47a5b6ba, Jun 28 2020, 08:53:46) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> a="Salesforcedrillers"
>>> print(a)
Salesforcedrillers
>>> del a[3]
Traceback (most recent call last):
  File "", line 1, in 
TypeError: 'str' object doesn't support item deletion
>>> del a
>>> print(a)
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'a' is not defined
>>>

Updation

>>> nat="Hello I am from USA"
>>> nat.replace("USA","India")
'Hello I am from India'
>>> print(nat)
Hello I am from USA
>>> nat=nat.replace("USA","India")
>>> print(nat)
Hello I am from India
>>>

Some Additional Feature
Conversion Functions:

Function Description
upper() Return a copy of string converting all lowercase letters of the string to uppercase.
lower() Return a copy of string converting all uppercase letters of the string to lowercase.
swapcase() Return a copy of string interchanging cases of letters.
capitalize() Return a copy of string converting all in Sentence format.
Python 3.8.4 (tags/v3.8.2:7b3ggab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
>>> name = 'sAlEs fOrCe dRiLleRs'
>>> print(name.upper())
SALES FORCE DRILLERS
>>> print(name.lower())
sales force drillers
>>> print(name.swapcase())
SaLeS FoRcE DrIlLErS
>>> print(name.capitalize())
Sales force drillers

Comparison Functions:

Function Description
isupper() Check whether all characters of string are in upper-case only.
islower() Check whether all characters of string are in lower-case only.
isdigit() Check whether all characters of string are numeric only.
isalpha() Check whether all characters of string are alphabet only.
isalnum() Check whether all characters of string are either alphabetic or numeric only.
Python 3.8.4 (tags/v3.8.2:7b3ggab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
>>> n1,n2,n3,n4 = 'sales', 'FORCE', 'drillersV2', '123'
>>> print(n1.isupper(), n2.isupper())
False True
>>> print(n1.islower(), n2.islower())
True False
>>> print(n3.isdigit(), n4.isdigit())
False True
>>> print(n2.isalpha(), n3.isalpha())
True False
>>> print(n2.isalnum(), n3.isalnum())
True True

String Operations

Operator Description
+ Used to join two strings, known as string concatenation.
* Used to repeat the elements of string, known as string replication.
in not in Used to test membership of substring in a given string. Return True/False
is is not Used to test the identity of two string objects.
Python 3.8.4 (tags/v3.8.2:7b3ggab59, Feb 25 2020, 23:03:10) [MSC v.1916 64 bit (AMD64)] on win32
>>> first= input("Enter your 1st name: ")
Enter your 1st name: salesforce
>>> last = input("Enter your second name: ")
Enter your 2nd name: drillers
>>> full = first+last
>>> print(full)
salesforcedrillers
>>> multiple = last * 2
>>> print(multiple)
drillersdrillers
>>> presence = 'sale' in full
>>> print(presence)
True
>>> absence = 'sales' not in full
>>> print(absence)
False
>>> absence = 'aws' not in full
>>> print(absence)
True
>>> company = 'salesforcedrillers'
>>> print(full is company)
True
Subscribe Now