print("hello">"Hello") #True
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.
+ operator can be used for concatenation and * is used for
multiplication.
e.g.
str = 'Hello World!' Test
print (str) # Prints complete string
print (str[0]) # Prints first character of the string
print (str[2:5]) # Prints characters starting from 3rd to 5th
print (str[2:]) # Prints string starting from 3rd character
print (str * 2) # Prints string two times
print (str + "TEST") # Prints concatenated string
print (str , "TEST") # Prints strings with a gap /space
print(len(str))
Answers Next slide
OUTPUT:-
Hello World!
H
llo
llo World!
Hello World!Hello World!
Hello World!TEST
Hello World! TEST
12
NOTE:- Python strings are "immutable" which means they cannot
be changed after they are created (Java strings also use this immutable
style).
Since strings can't be changed, we construct *new* strings as we go to
represent computed values.
e.g The expression ('hello' + 'there') takes in the 2 strings 'hello' and
'there' and builds a new string
'hellothere'.
To capitalize first character of the string “Comp”
Str&count.py
Str&count.py
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()
upper( ) --- Converts string into UPPERCASE s.upper()
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)
strip( )--- Removes whitespaces from start and end s.strip( )
print( "HEllo".lower( ))
print("hello".upper( ) )
print("abcdf".isalpha( ))
print("1233".isdigit( ))
print(" ".isspace( ))
print("hello".find('l’ ))
print("hello dear".replace('e','x’))
print("hello,dear,students".split(',’))
print("Welcome program".startswith('we'))
print("program".endswith("ram"))
list=["ramu","hari","shankar"]
s=','
print(s.join(list))
print(" hello dear ".strip( ))
a) print( "HEllo".lower( ))
b) print("hello".upper( ) ) a) hello
c) print("abcdf".isalpha( )) b) HELLO
d) print("1233".isdigit( )) c) True
e) print(" ".isspace( )) d) True
f) print("hello".find('l’ )) e) True
g) print("hello dear".replace('e','x’)) f) 2
h) print("hello,dear,students".split(‘,’)) g) hxllo dxar
i) print("Welcome program".startswith('we')) h) ['hello', 'dear', 'students']
j) print("program".endswith("ram")) i) False
list=["ramu","hari","shankar"] j) True
s=',’ k) ramu,hari,shankar
k) print(s.join(list)) l) hello dear
l) print(" hello dear ".strip( ))
my_string = "Hello World"
a) print(my_string.isalnum()) #check if all char are numbers
b) print(my_string.isalpha()) #check if all char in the string are alphabetic
c) print(my_string.isdigit()) #test if string contains digits
d) print(my_string.istitle()) #test if string contains title words
e) print(my_string.isupper()) #test if string contains upper case
f) print(my_string.islower()) #test if string contains lower case
g) print(my_string.isspace()) #test if string contains spaces
h) print(my_string.endswith('d')) #test if string endswith a d
i) print(my_string.startswith('H')) #test if string startswith H
my_string = "Hello World"
a) print(my_string.isalnum()) #check if all char are numbers
b) print(my_string.isalpha()) #check if all char in the string are alphabetic
c) print(my_string.isdigit()) #test if string contains digits a) False
b) False
d) print(my_string.istitle()) #test if string contains title words c) False
e) print(my_string.isupper()) #test if string contains upper case d) True
e) False
f) print(my_string.islower()) #test if string contains lower case f) False
g) print(my_string.isspace()) #test if string contains spaces
g) False
h) True
h) print(my_string.endswith('d')) #test if string endswith a d i) True
i) print(my_string.startswith('H’)) #test if string startswith H