fruit='apples'
l=len(fruit)
print 'Length of string:',l
print fruit[0:4] #[m:n] m is starting index and n will upto n-1 index
print fruit[-4] # Index starting from last element as -1, second last element as -2 so on
print fruit[::-1] # Reverse of a string
print fruit[:2]
print fruit[2:]
try:
print fruit[6]
except:
print 'index error'
for i in fruit:
print '\nChar in the string: ',i
for j in range(l):
print '\nIndex and Char in the string: ',j,fruit[j]
Solve Problems by Coding Solutions - A Complete solution for python programming
Compare the two string based on ascii values
str1=raw_input('Enter the string 1: ')
str2=raw_input('Enter the string 2: ')
if str1==str2:
print 'String are equal' # It is working based on ascii value refer http://www.ascii-code.com/
elif str1<str2:
print 'Str2 is greater than str1'
elif str1>str2:
print 'Str1 is greater than str2'
elif str1<=str2:
print 'Str1 is greater than or equal to str2'
elif str1>=str2:
print 'Str1 is greater than or equal to str2'
elif str1!=str2:
print 'Str1 is not equal to str2'
str2=raw_input('Enter the string 2: ')
if str1==str2:
print 'String are equal' # It is working based on ascii value refer http://www.ascii-code.com/
elif str1<str2:
print 'Str2 is greater than str1'
elif str1>str2:
print 'Str1 is greater than str2'
elif str1<=str2:
print 'Str1 is greater than or equal to str2'
elif str1>=str2:
print 'Str1 is greater than or equal to str2'
elif str1!=str2:
print 'Str1 is not equal to str2'
Subscribe to:
Posts (Atom)