Class 3
Class 3
Class 3 notes
+++++++++++++++++++++++
** String **
************
We can define string in Python using 2 way
i) single quote ('abc')
ii) double quote ("abc")
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]) #
%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!"
title() method returns the string all words first letter with capital
----------------------------------------------------------------
var = "Python, World!"
print(var.title())
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.
rfind(str, beg=0,end=len(string))
Same as find(), 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.