0% found this document useful (0 votes)
15 views

Class 3

The document provides an overview of string handling in Python. It discusses how to define strings, access characters using indexing and slicing, and various string operations and methods. These include formatting, stripping whitespace, checking length, converting case, replacing substrings, splitting into a list, and counting occurrences. The document lists many common string methods like isalpha(), isdigit(), lower(), upper(), replace(), split(), and more.

Uploaded by

Tarique Ejaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Class 3

The document provides an overview of string handling in Python. It discusses how to define strings, access characters using indexing and slicing, and various string operations and methods. These include formatting, stripping whitespace, checking length, converting case, replacing substrings, splitting into a list, and counting occurrences. The document lists many common string methods like isalpha(), isdigit(), lower(), upper(), replace(), split(), and more.

Uploaded by

Tarique Ejaz
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

+++++++++++++++++++++++

Class 3 notes
+++++++++++++++++++++++

Topics : String handling


-----------------------------
i. String indexing
ii. String slicing
iii. String operations
iv. String functions

** String **
************
We can define string in Python using 2 way
i) single quote ('abc')
ii) double quote ("abc")

var1 = 'Python World!'


var2 = "Python Programming"
print(var1)
print(var2)

String in Python also maintain indexing from 0 to (length - 1)

Access values in string


-----------------------

print(var1[0]) # Output: P

Range slice
-----------
var = 'Python World!'
print(var[2:5]) # Output: tho
print(var[2:]) # Output: thon World!
print(var[:5]) # Output: Pytho

name = 'Python'
print(name) # Print complete string
print (name[0]) # Print first character of the string
print (name[2:5]) # Print characters starting from 3rd to 5th
print (name[2:]) # Print string starting from 3rd character
print (name * 2) # Print string two times
print (name + "Institute") # Print concatenated string
print(name[0:6:2]) #

String formatting operator - %


==============================
print("Lets start %s. %d is year of Python" % ('Python', 2019))

%s string formatter
%d integer formatter

strip() method removes any whitespace from the beginning or the end
-------------------------------------------------------------------
var = " Python, World! "
print(var.strip()) # "Python, World!"

len() method returns the length of a string


-------------------------------------------
var = "Python, World!"
print(len(var)) # 14

lower() method returns the string in lower case


-----------------------------------------------
var = "Python, World!"
print(var.lower())

upper() method returns the string in upper case


-----------------------------------------------
var = "Python, World!"
print(var.upper())

capitalize() method returns the string first letter with capital


----------------------------------------------------------------
var = "Python, World!"
print(var.capitalize())

title() method returns the string all words first letter with capital
----------------------------------------------------------------
var = "Python, World!"
print(var.title())

replace() method replaces a string with another string


------------------------------------------------------
var = "Python, World!"
print(var.replace("P", "J"))

split() method splits the string into substrings


if it finds instances of the separator
------------------------------------------------
var = "Python, World!"
print(var.split(",")) # returns ['Python', ' World!']

count() method return number of occurance of a character


------------------------------------------------------
var = "this is string testing, really it is good";
substring = 'i'
#substring = 'is'
print (var.count('substring'))

find() method return position of first occurence


------------------------------------------------------
var = "this is an string example";
substring = "in";
print(var.find(substring))

startswith() & endswith() for check string start/end


----------------------------------------------------
var = 'this is for testing'
print(var.startswith('this'))
print(var.endswith('this'))

Some other Python built-in methods to manipulate strings


========================================================
isalnum()
Returns true if string has at least 1 character and all characters are alphanumeric and false
otherwise.

isalpha()
Returns true if string has at least 1 character and all characters are alphabetic and false otherwise.

isdigit()
Returns true if string contains only digits and false otherwise.

islower()
Returns true if string has at least 1 cased character and all cased characters are in lowercase and
false otherwise.

isnumeric()
Returns true if a unicode string contains only numeric characters and false otherwise.

isspace()
Returns true if string contains only whitespace characters and false otherwise.

istitle()
Returns true if string is properly "titlecased" and false otherwise.
isupper()
Returns true if string has at least one cased character and all cased characters are in uppercase and
false otherwise.

join(seq)
Merges (concatenates) the string representations of elements in sequence seq into a string, with
separator string.

len(string)
Returns the length of the string

maketrans()
Returns a translation table to be used in translate function.

max(str)
Returns the max alphabetical character from the string str.

min(str)
Returns the min alphabetical character from the string str.

replace(old, new [, max])


Replaces all occurrences of old in string with new or at most max occurrences if max given.

rfind(str, beg=0,end=len(string))
Same as find(), but search backwards in string.

rindex( str, beg=0, end=len(string))


Same as index(), but search backwards in string.

rjust(width,[, fillchar])
Returns a space-padded string with the original string right-justified to a total of width columns.

lstrip()
Removes all leading whitespace in string.

rstrip()
Removes all trailing whitespace of string.

strip([chars])
Performs both lstrip() and rstrip() on string.

split(str="", num=string.count(str))
Splits string according to delimiter str (space if not provided) and returns list of substrings; split into
at most num substrings if given.

splitlines( num=string.count('\n'))
Splits string at all (or num) NEWLINEs and returns a list of each line with NEWLINEs removed.

swapcase()
Inverts case for all letters in string.
isdecimal()
Returns true if a unicode string contains only decimal characters and false otherwise.

You might also like