Mod3_ppt
Mod3_ppt
String values in Python code begin and end with a single quote
A raw string completely ignores all escape characters and prints any
backslash that appears in the string
A multiline string in Python begins and ends with either three
single quotes or three double quotes
These expressions test whether the first string (the exact string,
case sensitive) can be found within the second string.
The upper() and lower() string methods return a new string
where all the letters in the original string have been converted to
uppercase or lowercase, respectively
These methods do not change the string itself but return new
string values
The isupper() and islower() methods will return a Boolean True
value if the string has at least one letter and all the letters are
uppercase or lowercase, respectively. Otherwise, the method
returns False.
upper() and lower() string methods themselves return strings.
• Along with islower() and isupper(), there are several string methods
• These methods return a Boolean value that describes the nature of the
string.
The isX String Methods
❖isalpha() returns True if the string consists only of letters and is not blank.
❖isalnum() returns True if the string consists only of letters and numbers
and is not blank.
❖isdecimal() returns True if the string consists only of numeric characters
and is not blank.
❖isspace() returns True if the string consists only of spaces, tabs, and new-
lines and is not blank.
❖istitle() returns True if the string consists only of words that begin with an
uppercase letter followed by only lowercase letters.
The isX String Methods
The isX string methods are helpful when you need to validate user
input. For example, the following program repeatedly asks users for their
age and a password until they provide valid input.
Output:
The startswith() and endswith() Methods
endswith() method returns True if the string value ends with the
string passed to the method
2 arguments
first argument to both methods is an integer length for the justified string
An optional second argument will specify a fill character other than a space
character
Justifying Text with the rjust(), ljust(), and
center() Methods
Justifying Text with the rjust(), ljust(), and
center() Methods
These methods are especially useful when you need to print
tabular data that has the correct spacing.
Output:
Removing Whitespace with the strip(),
rstrip(), and lstrip() Methods
The strip() string method will return a new string without any
whitespace characters at the beginning or end
Output:
The original string is : Computer Science
The reversed string(using recursion) is : ecneicS retupmoC
Reverse string (Using extended slice syntax)
Output:
The original string is : Bengaluru
The reversed string(using extended slice syntax) is : urulagneB
Reverse string (Using reversed)
Output:
The original string is : Python Programming
The reversed string(using reversed) is : gnimmargorP nohtyP
Python program to check if a string is palindrome or not
Method #1
1. Find reverse of string
2. Check if reverse and original are same or not.
Python program to check if a string is palindrome or not
Output:
Hello he said and went
Python Program to Display Fibonacci Sequence Using Recursion
Python Program to Find Sum of Natural Numbers Using Recursion
Output:
The sum is 1275
Python Program to Convert Decimal to Binary Using Recursion
Output:
100010
Inverted half pyramid using *and numbers
Program to print full pyramid using *
Program to print full pyramid using numbers
Reverse a Number using a while loop Reverse a Number Using String slicing
Output:
654321
Output:
4321
Python Program to Compute the Power of a Number using while loop, for
loop and pow() function
Output:
Answer = 81
Python Program to Count the Number of Occurrence
of a Character in String
Output:
2
Project: Password Locker
You probably have accounts on many different websites. It’s a bad habit to
use the same password for each of them because if any of those sites has a
security breach, the hackers will learn the password to all your other
accounts.
It’s best to use password manager software on your computer that uses
one master password to unlock the password manager. Then you can copy
any account password to the clipboard and paste it into the website’s
Password field.
The password manager program you’ll create in this example
isn’t secure, but it offers a basic demonstration of how such
programs work.
Project: Password Locker
and then ran the python program, the clipboard would then
contain the following:
Project: Adding Bullets to Wiki Markup
Python can be used to create, read, and save files on the hard
drive.
File and File Paths
File and File paths
function.
Absolute Path
Relative Path
directory
Absolute vs Relative Paths
the dot (.)
set to C:\bacon
The .\ at the start of a relative path is optional. For example, .\spam.txt and spam.txt
refer to the same file.
Creating New Folders Using the
os.makedirs() Function
On Windows, the drive, which is the single letter that often denotes a
The name of the file, made up of the stem (or base name) and the suffix (or
extension)
Windows Path objects have a drive attribute, but macOS and Linux Path objects
don’t
Getting the Parts of a File Path
Fig: The parts of a Windows (top) and macOS/Linux (bottom) file path
Getting the Parts of a File Path
Contd.,
os.path.split() does not take a file path and return a list of strings
of each folder.
Finding File Sizes and Folder Contents
The os.path module provides functions for finding the size of
a file in bytes and the files and folders inside a given folder.
os.path.exists('h:\\')
The File Reading/Writing Process
The File Reading/Writing Process
Binary files are all other file types, such as word processing
scrambled text
The File Reading/Writing Process
The pathlib module’s read_text()
method returns a string of the
full contents of a text file.
Close the file by calling the close() method on the File object
Opening Files with the open() Function
The open() function returns a File object