0% found this document useful (0 votes)
26 views2 pages

Datatypes in Python II 23 10 2024

Types of data

Uploaded by

visionias62
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views2 pages

Datatypes in Python II 23 10 2024

Types of data

Uploaded by

visionias62
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

What are Characters?

In [ ]: --> A character is the smallest unit of a string and is represented by an alphabet, digit, or special
symbol

--> Python treats characters as strings of length one.

Example of Characters:

name = 'Ansh' #It is a String of 4 Character


password = 'Pr@ 123' #It is a String of 7 Characters
student_name = 'Pratyush' #It is a String of 8 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.)

--> Indexing and Slicing is applicable with respect to Strings.

Examples of Strings
In [3]: name = 'Ansh' #4 Characters
print(type(name))
print(name)

<class 'str'>
Ansh

In [5]: rollno = "10" #2 Character


print(type(rollno))
print(rollno)

<class 'str'>
10

In [4]: college_name = '''Almabetter''' #10 Character


print(type(college_name))
print(college_name)

<class 'str'>
Almabetter

How to access Characters/Substring of a Strings?


In [ ]: --> Substring means Part of the String.

--> If we want to access Characters/Substring from a String then there are two ways:

1. Indexing ---> Accessing a Single Character

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.

--> String is ordered in Nature(Every Element is having it's own Index)

--> Types of Indexing:

1. Positive Indexing(Starting from 0) --> Forward Direction(Left to Right)


2. Negative Indexing(Starting from -1) --> Backend Direction(Right to Left)

Syntax:

string_name[index_value] #Square bracket is known as Accessing Operator

Important Note:

--> If index value is positive that means positive indexing(Forward Direction).

--> 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

IndexError: string index out of range

Builtin Methods of Strings

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)

In [14]: string1 = 'Python is Easy and Python is Open Source'


print(string1.index('Python')) #0
print(string1.index('o')) #4
print(string1.index(' ')) #6
print(string1.index('P')) #0
print(string1.index('Java')) #Error

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

ValueError: substring not found

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)

In [15]: string1 = 'Python is Easy and Python is Open Source'


print(string1.find('Python')) #0
print(string1.find('o')) #4
print(string1.find(' ')) #6
print(string1.find('P')) #0
print(string1.find('Java')) #-1

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)

In [17]: string1 = 'Python is Easy and Python is Open Source'


print(string1.count('Python'))#2
print(string1.count('Java'))#0

2
0

Write a Python Program that counts the Given Word Present in the Paragraph.
In [7]: paragraph = input('Enter Paragraph :')

word = input('Enter Word which You want to Count :')

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)

In [20]: string1 = 'Python is Easy and Python is Open Source'


print(string1.replace('Python','Java'))

Java is Easy and Java is Open Source

Write a Python Program that replace the given substring with the updated substring.
In [9]: paragraph = input('Enter Paragraph :')

older_substring = input('Enter the word which you want to replace :')

newer_substring = input('From which word you want to replace:')

ans = paragraph.replace(older_substring , newer_substring)


print("*"*100)
print('Paragraph is :',paragraph)
print("*"*100)
print('Intial Substring Which needs to replace is :',older_substring)
print("*"*100)
print('Updated Substring Which is going to replace initial substring :',newer_substring)
print("*"*100)
print('Updated Paragraph is :',ans)

****************************************************************************************************
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()

In [25]: string1 = ' Python is Easy '


print(string1)
print(string1.strip())

Python is Easy
Python is Easy

Case Level Methods

upper() Method
In [ ]: --> This upper method will convert all character of the given string in upper case.

Syntax:

string_name.upper()

In [26]: string1 = 'Python is High Level Programming Language'


print(string1.upper())

PYTHON IS HIGH LEVEL PROGRAMMING LANGUAGE

lower() Method
In [ ]: --> This lower method will convert all character of the given string in lower case.

Syntax:

string_name.lower()

In [27]: string1 = 'Python is High Level Programming Language'


print(string1.lower())

python is high level programming language

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()

In [28]: string1 = 'Python is high level Programming Language'


print(string1.title())

Python Is High Level Programming Language

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()

In [30]: string1 = 'python is High level Programming Language'


print(string1.capitalize())

Python is high level programming language

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()

In [31]: string1 = 'python is High level Programming Language'


print(string1.swapcase())

PYTHON IS hIGH LEVEL pROGRAMMING lANGUAGE

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()

In [32]: string1 = "Almabetter Education"


print(string1.isupper())

False

In [33]: string1 = "ALMABETTER EDUCATION"


print(string1.isupper())

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()

In [34]: string1 = "Almabetter Education"


print(string1.islower())

False

In [35]: string1 = "almabetter education"


print(string1.islower())

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()

In [36]: string1 = "Almabetter Education"


print(string1.isalpha())

False

In [37]: string1 = "AlmabetterEducation"


print(string1.isalpha())

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()

In [38]: string1 = "12344"


print(string1.isdigit())

True

In [39]: string1 = "123 44"


print(string1.isdigit())

False

How to Write String Content in Multiple Lines


In [ ]: --> If we want to write string content in multiple lines that we will only use Triple Quotation only.

--> We can't use Single and Double Quotation for writing string content into multiple lines.

In [40]: s = "Python is a high-level, general-purpose programming language.


Its design philosophy emphasizes code readability with the use of significant indentation.
Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including structured, object-oriented and functional programming."

File "<ipython-input-40-704e3229136a>", line 1


s = "Python is a high-level, general-purpose programming language.
^
SyntaxError: unterminated string literal (detected at line 1)

In [41]: s = '''Python is a high-level, general-purpose programming language.


Its design philosophy emphasizes code readability with the use of significant indentation.
Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including structured, object-oriented and functional programming.'''

print(s)

Python is a high-level, general-purpose programming language.


Its design philosophy emphasizes code readability with the use of significant indentation.
Python is dynamically typed and garbage-collected. It supports multiple programming paradigms, including structured, object-oriented and functional programming.

String Formatting in Python


In [ ]: --> If we want to inject a variable value into the string then we will use String Formatting Concept.

--> It is the process of inserting a custom string or variable in predefined text.

Syntax:

1. f'text...{variable_name}....text'

2. 'text....{}..{}...'.format(variable_name1,variable_name2)

In [44]: Name = 'Pratyush'


Platform = 'Almabetter'
print(f'Hi How are {Name} , Welcome to {Platform}')

Hi How are Pratyush , Welcome to Almabetter

In [45]: Name = 'Pratyush'


Platform = 'Almabetter'
print('Hi How are {} , Welcome to {}'.format(Name,Platform))

Hi How are Pratyush , Welcome to Almabetter


In [ ]:

You might also like