Datatypes in Python II 23 10 2024
Datatypes in Python II 23 10 2024
In [ ]: --> A character is the smallest unit of a string and is represented by an alphabet, digit, or special
symbol
Example of Characters:
String Datatype
In [ ]: --> String is a sequence/Collection of Characters.
--> In Python, Anything within Single Quotes,Double Quotes or Triple Quotes is nothing but a String.
--> In case of Python Text is also represented in the form Strings only.
--> String is ordered in Nature.(Each and Every Characters are having there fixed positions.)
Examples of Strings
In [3]: name = 'Ansh' #4 Characters
print(type(name))
print(name)
<class 'str'>
Ansh
<class 'str'>
10
<class 'str'>
Almabetter
--> If we want to access Characters/Substring from a String then there are two ways:
2. Slicing ---> A part of the String or Substring(Access Single Char or Group of Character)
Indexing in Python
In [ ]: --> if we want to access a Single Character from a String then we will use Indexing.
Syntax:
Important Note:
--> if the index value is negative that means Negative Indexing(Backword Direction)
--> if the index value is not present we will get index Error.
Example of Indexing
In [9]: string1 = 'Python'
print(string1[3]) #h
print(string1[-3]) #h
print(string1[0]) #P
print(string1[-5]) #y
print(string1[200]) #error
h
h
P
y
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-9-1a9548486c2d> in <cell line: 6>()
4 print(string1[0]) #P
5 print(string1[-5]) #y
----> 6 print(string1[200]) #error
index() Method
In [ ]: --> This method is used to return the index of First occurance of the Given Substring.
--> If the Given Substring is not present then we will get an Value Error.
Syntax:
string_name.index(substring)
0
4
6
0
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-14-a56df83a10fb> in <cell line: 6>()
4 print(string1.index(' ')) #6
5 print(string1.index('P')) #0
----> 6 print(string1.index('Java')) #Error
Find() Method
In [ ]: --> This method is used to return the index of First occurance of the Given Substring.
--> If the Given Substring is not present then we will get an answer -1.
Syntax:
string_name.find(substring)
0
4
6
0
-1
Count() Method
In [ ]: --> This method is used to find the number of occurances of the Given Substring.
Syntax:
string_name.count(substring)
2
0
Write a Python Program that counts the Given Word Present in the Paragraph.
In [7]: paragraph = input('Enter Paragraph :')
ans = paragraph.count(word)
print("*"*100)
print('Paragraph is :',paragraph)
print("*"*100)
print('Word is :',word)
print("*"*100)
print('Given Word is present',ans,'number of times in the Given Paragraph')
****************************************************************************************************
Paragraph is : Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation. Python is dynamically t
yped and garbage-collected. It supports multiple programming paradigms, including structured, object-oriented and functional programming.
****************************************************************************************************
Word is : Python
****************************************************************************************************
Given Word is present 2 number of times in the Given Paragraph
Replace() Method
In [ ]: --> This Method is used to replace the older substring with the newer substring.
Syntax:
string_name.replace(older_substring , newer_substring)
Write a Python Program that replace the given substring with the updated substring.
In [9]: paragraph = input('Enter Paragraph :')
****************************************************************************************************
Paragraph is : Python is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation. Python is dynamically t
yped and garbage-collected. It supports multiple programming paradigms, including structured, object-oriented and functional programming.
****************************************************************************************************
Intial Substring Which needs to replace is : Python
****************************************************************************************************
Updated Substring Which is going to replace initial substring : Java
****************************************************************************************************
Updated Paragraph is : Java is a high-level, general-purpose programming language. Its design philosophy emphasizes code readability with the use of significant indentation. Java is dynamical
ly typed and garbage-collected. It supports multiple programming paradigms, including structured, object-oriented and functional programming.
strip() Method
In [ ]: --> This method is used to remove extra whitespaces(left side as well as right side) from the string.
Syntax:
string_namne.strip()
Python is Easy
Python is Easy
upper() Method
In [ ]: --> This upper method will convert all character of the given string in upper case.
Syntax:
string_name.upper()
lower() Method
In [ ]: --> This lower method will convert all character of the given string in lower case.
Syntax:
string_name.lower()
Title() Method
In [ ]: --> This method is used to convert First Character of each word of the String in Upper Case and rest all in lower case.
Syntax:
string_name.title()
Given a Name of a Person Convert First Name and Last Name of the Person in Upper Case.
In [29]: name = 'pratyush Srivastava'
ans = name.title()
print(ans)
Pratyush Srivastava
Capitalize() Method
In [ ]: --> This Method is used to Convert first character of the string in upper case and rest all in lower case.
Syntax:
string_name.capitalize()
Swapase() Method
In [ ]: --> This method will convert upper character to lower character and lower character to upper character of the string.SyntaxError
Syntax:
string_name.swapcase()
isupper() Method
In [ ]: --> This method will return result in the form of boolean Datatype(True or False)
--> This method will return True if all characters of the given String is in upper case else this method will return
False.
Syntax:
string_name.isupper()
False
True
islower() Method
In [ ]: --> This method will return result in the form of boolean Datatype(True or False)
--> This method will return True if all characters of the given String is in lower case else this method will return
False.
Syntax:
string_name.islower()
False
True
isalpha() Method
In [ ]: --> This method will return result in the form of boolean Datatype(True or False)
--> This method will return True if all characters of the given String are Alphabet else this method will return
False.
Syntax:
string_name.isalpha()
False
True
isdigit() Method
In [ ]: --> This method will return result in the form of boolean Datatype(True or False)
--> This method will return True if all characters of the given String are digits else this method will return
False.
Syntax:
string_name.isdigit()
True
False
--> We can't use Single and Double Quotation for writing string content into multiple lines.
print(s)
Syntax:
1. f'text...{variable_name}....text'
2. 'text....{}..{}...'.format(variable_name1,variable_name2)