Module 2 - Data Manipulation and Input

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Click to edit Master title style

Data
Manipulation
and Input
Module 2

1
Click to edit Master title style
Outline:

• Data Input
• String and Number Manipulation
• Placeholders
• String Methods

2
ClickUser
The to edit Master title style
Input

• The input() function is a part of the core library of standard Python


distribution
• It reads the key strokes as a string object which can be referred to
by a variable having a suitable name

3
ClickUser
The to edit Master title style
Input

• The input() function takes the user's input from the next line
• The input() will capture it and assign it to a variable if assigned
• The variable will display whatever the user has provided as the
input
• The input() function always reads the input as a string, even if
comprises of digits

4
Click to edit[04]
Simulation Master title style

5
Click to String
Python edit Master title style

• In Python, string is an immutable sequence data type


• It is the sequence of Unicode characters wrapped inside single,
double, or triple quotes
• It can also be assigned to a variable
• Multi-line strings must be embed in triple quotes

6
Click to String
Python edit Master title style

• If a string literal required to embed double quotes as part of a


string then, it should be put in single quotes
• Likewise, if a string includes a single quote as a part of a string
then, it should be written in double quotes
• You can use the len() function to retrieve the length of a string
• len(“Hello”)
• str1 = “Hello”
• len(str1)

7
Click toSequence
String edit Master title style

• A sequence is defined as an ordered collection of items


• Hence, a string is an ordered collection of characters
• The sequence uses an index, starting with zero to fetch a certain
item (a character in case of a string) from it
• Python supports negative indexing too, starting with -(length of
string) till -1

8
Click to editNature
Immutable Masteroftitle style
String

• The string is an immutable object


• Hence, it is not possible to modify it
• The attempt to assign different characters at a certain index
results in errors

9
Clickstr
The to Class
edit Master title style

• All strings are objects of the str class in Python


• Use the str() function to convert a number to a string

10
Click to Sequences
Escape edit Master title style

• The escape character is used to invoke an alternative


implementation of the subsequent character in a sequence
• In Python, backslash \ is used as an escape character
• Use a backslash character followed by the character you want to
insert in a string e.g. \' to include a quote, or \" to include a double
quotes in a string

11
Click to Sequences
Escape edit Master title style

12
Click toOperators
String edit Master title style

13
Click to edit[05]
Simulation Master title style

14
Clickformat()
The to edit Master
Methodtitle style

The format() method formats the specified value(s) and insert them
inside the string's placeholder

The placeholder is defined using curly brackets: {}

The format() method returns the formatted string.

15
ClickPlaceholders
The to edit Master title style

The placeholders can be identified using named indexes {price},


numbered indexes {0}, or even empty placeholders {}.

16
Click to edit[06]
Simulation Master title style

17
Click toMethods
String edit Master title style

• str.capitalize() Returns the copy of the string with its first character capitalized and the rest of the letters are

in lowercase
• string.casefold() Returns a lowered case string. It is similar to the lower() method, but the casefold()

method converts more characters into lower case.


• string.center() Returns a new centered string of the specified length, which is padded with the specified

character. The default character is space.


• string.count() Searches (case-sensitive) the specified substring in the given string and returns an integer

indicating occurrences of the substring.


• string.endswith() Returns True if a string ends with the specified suffix (case-sensitive), otherwise

returns False.
18
Click toMethods
String edit Master title style

• string.find() Returns the index of the first occurence of a substring


in the given string (case-sensitive). If the substring is not found it
returns -1
• string.index() Returns the index of the first occurence of a substring
in the given string.
• string.islower() Checks whether all the characters of a given string
are lowercased or not. It returns True if all characters are lowercased
and False even if one character is uppercase.
• string.istitle() Checks whether each word's first character is upper
case and the rest are in lower case or not. It returns True if a string is
titlecased; otherwise, it returns False. The symbols and numbers are
ignored.
19
Click toMethods
String edit Master title style

• string.isupper() Returns True if all characters are uppercase and


False even if one character is not in uppercase.
• string.join() Returns a string, which is the concatenation of the
string (on which it is called) with the string elements of the
specified iterable as an argument.
• string.lower() Returns the copy of the original string wherein all
the characters are converted to lowercase.
• string.upper() Returns a string in the upper case. Symbols and
numbers remain unaffected.
20
Click to edit[07]
Simulation Master title style

21

You might also like