PDA Assignments
  • Python For Data Analytics
    • 1.Python
      • 1.Python Documents
        • 1.Data Types
        • 2.Variables In Python
        • 3.Operators In Python
        • 4.User Input In Python
        • 5.TypeCasting In Python
        • 6.Strings In Python
        • 7.Conditional Statements In Python
        • 8.Branching using Conditional Statements and Loops in Python
        • 9.Lists In Python
        • 10.Sets In Python
        • 11.Tuples In Python
        • 12.Dictionary In Python
        • 13.Functions In Python
        • 14.File Handling In Python
        • 15.Numerical Computing with Python and Numpy
      • 2.Python Assignments
        • Data Type & Variables
        • Operators Assignment
        • User Input & Type Casting
        • Functions- Basic Assignments
        • String Assignments
          • String CheatSheet
        • Conditional Statements Assignments
        • Loops Assignments
        • List Assignments
          • List Cheatsheet
        • Set Assignments
          • Sets Cheatsheet
        • Dictionary Assignments
          • Dictionary Cheatsheet
        • Function Assignments
        • Functions used in Python
      • 3.Python Projects
        • Employee Management System
        • Hamming distance
        • Webscraping With Python
          • Introduction To Web Scraping
          • Importing Necessary Libraries
          • Basic Introduction To HTML
          • Introduction To BeautifulSoup
          • Flipkart Web Scraping
            • Scraping Step By Step
        • Retail Sales Analysis
        • Guess the Word Game
        • Data Collection Through APIs
        • To-Do List Manager
        • Atm-functionalities(nested if)
        • Distribution of Cards(List & Nested for)
        • Guess the Number Game
      • 4.Python + SQL Projects
        • Bookstore Management System
    • 2.Data Analytics
      • 1.Pandas
        • 1.Pandas Documents
          • 1.Introduction To Pandas
          • Reading and Loading Different Data
          • 2.Indexing and Slicing In Pandas
          • 3.Joining In Pandas
          • 4.Missing Values In Pandas
          • 5.Outliers In Pandas
          • 6.Aggregating Data
          • 7.DateTime In Pandas
          • 8.Validation In Pandas
          • 9.Fetching Data From SQL
          • 10. Automation In Pandas
          • 11.Matplotlib - Data Visualization
          • 12. Seaborn - Data Visualization
          • 13. Required Files
        • 3.Pandas Projects
          • Retail Sales Analysis
            • Retail Sales Step By Step
          • IMDB - Dataset Analysis - Basic
        • 2. Pandas Assignments
          • 1. Reading and Loading the Data
          • 2. Data frame Functions and Properties
          • 3. Series - Basic Operations
          • 4. Filtering in Pandas
          • 5. Advance Filtering
          • 6. Aggregate Functions & Groupby
          • 7. Pivot Tables
          • 8. Datetime
          • 9. String Functions
Powered by GitBook
On this page
  • What is String?
  • Single Line String Vs Multiline String:
  • Memory Allocation of Numbers Vs Strings:
  • Indexing of Strings:
  • Positive Index Numbers :
  • To Access characters from a string (+ve indexing):
  • Negative Index Numbers:
  • To Access Characters from string (-ve indexing):
  • Slicing of a String:
  • Slicing of string with steps:
  • Default Value of Starting Index , Ending index and Step size:
  • Negative Slicing of a string:
  • String formatting:
  • Functions used in String :
  • String Methods :
  • 1.lower() Method:
  • 2.upper() Method:
  • 3.swapcase() Method:
  • 4.title() Method :
  • 5.capitalize() Method:
  • 6.strip(),lstrip() and rstrip() Method:
  • 7.replace() Method:
  • 8.count() method:
  • 9.find() Method:
  • 10.rfind() method:
  • 11.index() and rindex() method:
  • Difference between find() and index() Method:
  • 12.split() and rsplit() Method :
  • Difference between split() and rsplit():
  • 13.partition() and rpartition() method:
  • Methods that returns only Boolean Values(True or False):
  • 14.islower() method:
  • 15.isupper() method:
  • 16.isalpha() method:
  • 17.isnumeric() method:
  • 18.isalnum() method:
  • 19.startswith() method:
  • 20.endswith() method:
  1. Python For Data Analytics
  2. 1.Python
  3. 1.Python Documents

6.Strings In Python

Previous5.TypeCasting In PythonNext7.Conditional Statements In Python

Last updated 1 year ago

What is String?

Python string is the collection or sequence of the characters surrounded by single quotes(' '), double quotes(" "), or triple quotes(''' '''),(""" """).

In more simpler terms , whatever you write between these quotes are String. For Example:</span>

In [ ]:

Company = "Console Flare"
member = '500'
year = '''4'''
Sector = """IT"""
print("Data type of company: ",type(Company))
print("Data type of member: ",type(member))
print("Data type of year: ",type(year))
print("Data type of sector: ",type(Sector)) 

Single Line String Vs Multiline String:

Single line strings are written under single quotes '' and double quotes "". They appear as a single line on console screen. For Example:

In [3]:

Sentence = "Python is a General Purpose programming language"
print(Sentence)
Python is a General Purpose programming language

Multiline line strings are written under triple quotes ''' ''' and """ """. They appear in multiple lines such as paragraphs. For Example:

In [6]:

Sentence = '''Python is 
a general purpose
Programming Language'''
print(Sentence)
Python is 
a general purpose
Programming Language

Memory Allocation of Numbers Vs Strings:

Memory allocation of Numbers : A number (integer or float) gets stored in a memory block and gets associated with a variable as soon as we define it. For Example:

In [ ]:

age = 24

Memory Allocation of Strings : As we know that strings are a sequence of characters, hence Each character of string gets store in different memory block, something like this:

In [ ]:

name = "ABHI"

Indexing of Strings:

As we know that characters of strings are stored in different memory blocks, each character within a string is given an index number.

Python offers two different indexing :

Positive Index Numbers : Positive index numbers starts from zero (0) and left to right.

Positive Index Numbers :

To Access characters from a string (+ve indexing):

As we know that characters of a string are identified by their index number, so we can access characters with the help of character's index numbers.

For Example in name = 'abhi', to get character 'a' from a string 'abhi' , we will type name[0].

In [8]:

name = "abhi"
name[0]
a

Negative Index Numbers:

To Access Characters from string (-ve indexing):

Suppose there is a long string and we need to access character at the end of the string, instead of using positive indexing , negative indexing would be much more helpful. For Example:

In [10]:

sentence = "Python is a Genreal Purpose Programming LanguagE"

To get 'E' from sentence with the help of Positive indexing would be tough rather than accessing character with the help of negative indexing.

In [11]:

sentence[-1]

Out[11]:

'E'

Slicing of a String:

Slicing of a string means taking a substring or part of a string from it. For example , 'Console' is a substring of 'Console Flare'. Index numbers should always be in ascending orders.

Slicing of a string is done like this : variable[starting index : ending index]. Here ending index should be one index number more than the last character's index number.

In more simpler terms in name = "abhishek", "abhi" from "abhishek" can be sliced as name[0:4].

In [12]:

company = "Console Flare"
company[0:7]

Out[12]:

'Console'

Slicing of string with steps:

variable[starting index : ending index : step size(n)], here steps mean, it takes every n step starting from starting index. Default value of step is 1.

In [13]:

company = "console flare"
company[0:14:2]

Out[13]:

'cnoefae'

Default Value of Starting Index , Ending index and Step size:

Default Value of Starting index is 0.If we don't write anything as Starting index while slicing , it takes 0 as default. For Example:

In [14]:

company = "Console Flare"
company[:7]

Out[14]:

'Console'

Default Value of Ending index is end of string.If we don't write anything as ending index while slicing , it takes end of string as indexing. For Example:

In [15]:

company = "Console Flare"
company[0:]

Out[15]:

'Console Flare'

Default Value of Step size is 1 and is optional , we need to use step size only if we want it to skip characters in a string.

In [16]:

company = "Console Flare"
company[::3]

Out[16]:

'Csele'

Negative Slicing of a string:

Negative slicing of a string means to slice the string with the help of negative indexes. Here also , index numbers should always be in ascending order. For Example:

In [18]:

company = "console flare"
company[-13:-6]

Out[18]:

'console'

String formatting:

formatting strings lets you use embedded Python expressions inside string. Here’s a simple example to give you a feel for the feature:

In [86]:

birthyear = 1997
presentage = f'my present age is {2021-birthyear}'
print(presentage)
my present age is 24

f'string and expressions': This is how you can merge expressions and string in a single string.

Functions used in String :

len() function:

len() function is used to count characters in a string. For Example:In [19]:

sentence = "String is a sequence of characters"
len(sentence)

Out[19]:

34

max() function:

max() function is used to return largest value or character in a string , For Example:In [22]:

sentence = '1234542398'
max(sentence)

Out[22]:

'9'

In [23]:

sentence = 'Console Flare'
max(sentence)

Out[23]:

's'

ASCII Code of 's' is 115 , largest than any other character's.

min() function:

min() function is used to return smallest value or character in a string , For Example:In [25]:

sentence ='123'
min(sentence)

Out[25]:

'1'

String Methods :

To Understand String Methods better , first we will have to know what are methods.

What are Methods?

Methods are nothing but Type - Specific Function. Methods are functions that only work for a particular data type.

In more Simpler terms, String methods are functions that will only work for strings. String methods are used for handling strings , performing operations on string , string manipulation etc.

Note: All String methods return new values and does not change original string.

1.lower() Method:

lower() method is used to return string with all its character in lowercase. We do not pass any paramters in lower(). For Example:

In [27]:

company = "CONSOLE FLARE"
var = company.lower()
print(var)
console flare

2.upper() Method:

upper() method is used to return string with all its character in Capital case. We do not pass any paramters in upper(). For Example:

In [28]:

company = "console flare"
var = company.upper()
print(var
CONSOLE FLARE

3.swapcase() Method:

swapcase() method is used to return string with all its character in lower to upper case and all its string in upper to lower case. It does not have any parameters. For Example:

In [29]:

company = "CoNSole FLAre"
var = company.swapcase()
print(var)
cOnsOLE flaRE

4.title() Method :

The title () method in python is used to convert the first character in each word to Uppercase and remaining characters to Lowercase in the string and returns a new string. It does not have any paramters. For Example:

In [31]:

sentence = "pyThon is a geneRAL PuRPOSE programming Language.It is invented by Guido van rossum."
var = sentence.title()
print(var)
Python Is A General Purpose Programming Language.It Is Invented By Guido Van Rossum.

5.capitalize() Method:

capitalize() method return string with its first character as upper case and rest in lower case. It does not have any parameters. For Example:

In [32]:

sentence = "pyThon is a geneRAL PuRPOSE programming Language.It is invented by Guido van rossum."
var = sentence.capitalize()
print(var)
Python is a general purpose programming language.it is invented by guido van rossum.

6.strip(),lstrip() and rstrip() Method:

strip() method removes all the whitespace from left and right side of the string.

lstrip() method removes all the whitespace from left side of the string.

rstrip() method removes all the whitespace from right side of the string.

In [81]:

company = "  console flare  "
var = company.strip()
print(company)
print(var)
  console flare  
console flare

In [82]:

company = "  console flare  "
var = company.lstrip()
print(company)
print(var)
  console flare  
console flare  

It removed all whitespaces from left side of string.

In [84]:

company = "console flare  "
var = company.rstrip()
print(company)
print(var)
console flare  
console flare

It removed all whitespaces from right side of string.

7.replace() Method:

replace() method returns a string where a specified value is replace with a specified value.

Parameters to be passed in replace:

oldvalue: The string to be replaced. It is Required

newvalue : The string to replace the old value with. It is Required

count: An integer value specifying how many occurences of the old string you want to replace. Default value of count is All Occurences. It is Optional

replace(oldvalue,newvalue,count)

In [35]:

sentence = "I am learning C Language C Language."

To replace 'C Language' with 'Python Programming Language' , we will use replace() method.

In [36]:

sentence = sentence.replace('C Language','Python Programming Language')
print(sentence)
I am learning Python Programming Language Python Programming Language.

To see , How count works, i will be using count parameter in below example:

In [37]:

sentence = "I am learning C Language C Language."
sentence = sentence.replace('C Language','Python Programming Language',1)
print(sentence)
I am learning Python Programming Language C Language.

8.count() method:

count() method returns the number of times a specified value occurs in a string.

Parameters to be passed in count():

value : substring or value to count. It is Required.

start index : The position from where to start the search. It is Optional. Default value is 0.

end index : The position from where to end the search.It is Optional. Default value is the end of the string.

count(value,start,end)

In [41]:

sentence = "Python is Python and C is C"
var = sentence.count("Python")
print(var)
2

To see , How start and end paramters work , i will be using them in below example:

In [40]:

sentence = "Python is Python and C is C"
var = sentence.count("Python",1,16)
print(var)
1

9.find() Method:

find() method searches the string for a specified value and returns the starting position of where it is found. It searches the whole string for specified value from left to right.

In more simpler terms, it finds the string's position.

Parameters to be passed inside find() method:

value : The values to search for. It is Required.

start : Starting index, from where to look for the value. It is Optional

end : Ending index, from where to stop the search for value. It is Optional

find(value,start,end)

In [45]:

sentence =  "Python is general purpose programming language."
lookfor = sentence.find("Python")
print(lookfor)
0

To see , How start and end paramters work , i will be using them in below example:

In [47]:

sentence =  "Python is Python and C is C"
lookfor = sentence.find("Python",1,19)
print(lookfor)
10

Hence, the operation of start and end is same everywhere, it looked for 'python' from index number 1 to 18.

10.rfind() method:

rfind() method is same as find() method with same parameters. The only difference is it looks for specified string from right to left. For Example:

In [49]:

sentence =  "Python is Python and C is C"
lookfor = sentence.rfind("Python")
print(lookfor)
10

11.index() and rindex() method:

index() method also searches the string for a specified value and returns the position of where it is found.

In more simpler terms index() works like find() and rindex() works like rfind(). They both have same parameters. For Example:

In [50]:

sentence =  "Python is Python and C is C"
lookfor = sentence.index("Python")
print(lookfor)
0
sentence =  "Python is Python and C is C"
lookfor = sentence.rindex("Python")
print(lookfor)
10

Difference between find() and index() Method:

find() does not give error, when value or substring is not found and flow of the program does not interrupt.

index() gives error when value or substring is not found. for Example:

In [53]:

sentence =  "Python is Python and C is C"
lookfor = sentence.find("Java")
print(lookfor)
-1

In [55]:

sentence =  "Python is Python and C is C"
lookfor = sentence.index("Java")
print(lookfor)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-55-7829979dbdb0> in <module>
      1 sentence =  "Python is Python and C is C"
----> 2 lookfor = sentence.index("Java")
      3 print(lookfor)

ValueError: substring not found

12.split() and rsplit() Method :

split() method splits the string at the specified separator and returns a list(We will cover list in upcoming lessons. It is another data type.)

Parameters to be passed in split():

separator : Specifies the separator to use when splitting the string. It is Optional.By Default , any whitespace is separator.

maxsplit : maxsplit specifies how many splits to do . It is Optional.It's Default value is -1 or All Occurences

In [56]:

sentence = "Python is a general purpose programming language"
var = sentence.split(" ")
print(var)
['Python', 'is', 'a', 'general', 'purpose', 'programming', 'language']

To see , How maxsplit work , i will be using them in below example:

In [57]:

sentence = "Python is a general purpose programming language"
var = sentence.split(" ",2)
print(var)
['Python', 'is', 'a general purpose programming language']

rsplit() works like split() , it also splits the string from right to left. For Example:

In [58]:

sentence = "Python is a general purpose programming language"
var = sentence.rsplit(" ")
print(var)
['Python', 'is', 'a', 'general', 'purpose', 'programming', 'language']

Difference between split() and rsplit():

if you use split() and rsplit() method on a same string you will find there is no difference in output. let's use them both on a single string:

In [59]:

sentence = "This is Console Flare Python class"
var = sentence.split(" ")
print(var)
['This', 'is', 'Console', 'Flare', 'Python', 'class']
sentence = "This is Console Flare Python class"
var = sentence.rsplit(" ")
print(var)
['This', 'is', 'Console', 'Flare', 'Python', 'class']

split() splits the string from left to right and rsplit() splits the string from right to left , yet output is same always.

Difference between split() and rsplit() can only be shown with maxsplit(). let's follow these examples:

In [63]:

sentence = "This is Console Flare Python class"
var = sentence.split(" ",2)
print(var)
['This', 'is', 'Console Flare Python class']

In [64]:

sentence = "This is Console Flare Python class"
var = sentence.split(" ",2)
print(var)
['This', 'is', 'Console Flare Python class']

Because rsplit() splits the string from right to left , maxsplit gives different result from split() method.

13.partition() and rpartition() method:

partition() method returns string where string is parted into three parts:

1.Everything before the first separator 2.Separator itself. 3.Everything after the first separator.

Parameter to be passed into method: separator

partition(separator)

In [66]:

sentence = "python is Cool"
var = sentence.partition(" ")
print(var)
('python', ' ', 'is Cool')

Note:rpartition() is same as partition() , only it starts from right to left.In [67]:

sentence = "python is Cool"
var = sentence.rpartition(" ")
print(var)
('python is', ' ', 'Cool')

Methods that returns only Boolean Values(True or False):

14.islower() method:

islower() returns True if all the characters in string is lowercase , otherwise it returns False. It has no Parameters.

In [68]:

company = 'console flare'
var = company.islower()
print(var)
True

15.isupper() method:

isupper() returns True if all the characters in string is uppercase , otherwise it returns False. It has no Parameters.

In [69]:

company = 'CONSOLE FLARE'
var = company.isupper()
print(var)
True

16.isalpha() method:

isalpha() returns True if all the characters in string is only alphabets , otherwise it returns False. It has no Parameters.

In [70]:

company = 'CONSOLEFLARE'
var = company.isalpha()
print(var)
True

17.isnumeric() method:

isnumeric() returns True if all the characters in string is only numbers , otherwise it returns False. It has no Parameters.

In [72]:

num = "15"
var = num.isnumeric()
print(var)
True

18.isalnum() method:

isalnum() returns True if all the characters in string is either number or string or both , otherwise it returns False. It has no Parameters.

In [73]:

num = "123abc"
var = num.isalnum()
print(var)
True

19.startswith() method:

startswith() returns true if the string starts with a specified value.

Parameters to be Passed in method:

value : The values to check if the string starts with. It is Required

start : An integer specifying at which position to start the search. It is Optional

end : An integer specifying at which position to end the search. It is Optional

In [74]:

name = "Console flare"
var = name.startswith("Con")
print(var)
True

20.endswith() method:

endswith() returns true if the string ends with a specified value.

Parameters to be Passed in method:

value : The values to check if the string ends with. It is Required

start : An integer specifying at which position to start the search. It is Optional

end : An integer specifying at which position to end the search. It is Optional

In [75]:

name = "Console flare"
var = name.endswith("e")
print(var)
True

max() function can also return largest alphabetic character based on their ascii code. You can go through ASCII codes of different alphabets through . For Example:

here