PYTHON (STRING)
PYTHON (STRING)
print("bcd"<"bad") #False
print("ABC">="ABC") # True
print("ABCD"=="ABCDE") # False
print("hello"!="Hello") # True
print("hello"<="Hello") # False
Like other languages, String is indexed here and it’s indexes starting at
0 in the beginning of the string and working their way from -1 at the
end. String in python can also be accessed with negative indexed in
reverse order.
Strfunc.py
(‘my’, ‘comp’ )
# string functions
s=" welcome dear " 14
print(len(s)) 13
print(len(s.lstrip())) 13
print(len(s.rstrip())) 12
print(len(s.strip()))
s1="python-programming"
print(s1.lstrip('typ')) hon-programming
print(s1.lstrip('py')) thon-programming
print(s1.lstrip('th')) python-programming
print()
s2="Beautiful" Beautiful
print(s2.lstrip('bea'))
print(s2.lstrip('eB'))
autiful
print(s2.lstrip('Bear’)) utiful
print(s2.lstrip('utfear')) Beautiful
print(s2.lstrip('Beartu'))
print()
iful
s3="Handsome" Handsome
print(s3.rstrip('om')) Hands
print(s3.rstrip('Some'))
print(s3.rstrip('me')) Handso
print(s3.rstrip('domee')) Hands
print(s3.rstrip('ddoma')) Handsome
print(s3.rstrip('esadomme'))
print(s3.rstrip('esdomme')) Han
Han
lower( ) --- Converts string into lowercase s.lower()
isalpha( ) --- tests if all the string chars are in characters only. s.isalpha()
isdigit( ) --- tests if all the string chars are in digit only. s.isdigit()
isspace( )--- tests if all the string chars are in spaces s.isspace()
find( )--- searches for the given other string (not a regular expression) within
s, and returns the first index where it begins or -1 if not found
s.find('other')
replace( )---returns a string where all occurrences of 'old' have been replaced by 'new'
s.replace ('old', 'new')
split( )--- returns a list of substrings separated by the given delimiter s.split('delim').
startswith( )--- Check whether that string starts with a particular string s.startswith(‘A‘)
endswith( )--- Check whether that string ends with a particular string s.endswith(‘A‘)
join( )--- opposite of split(), joins the elements in the given list together using the string as the
delimiter. s.join(list)