Answers:: Operations ON String Variables
Answers:: Operations ON String Variables
1. Write a program to compare two strings the program should not consider case(means case
insensitive). The output would be "Both are same " if they are same otherwise "Both are not
same".
2. Consider,a string x="This is python programmer learning python". Write a program to get
sub string "python"and check how many times the string is repeated.
3. Write a program to trim the leading and trialing spaces in a string taken from user as input.
4. Write a program that adds and multiplies two complex numbers and shows output on screen
as a complex number and also real and imaginary numbers seperately.
Answers:
1.
#!/usr/bin/python
x=input('Enter first string:')
y=input('Enter second string:')
if x==y:
print ("Both are same")
else:
print ("Both are not same")
2.
#!/usr/bin/python
x="This is python programmer learning python"
y=x[8:14]
print (x.count(y))
3.
#!/usr/bin/python
x=input('Enter string:')
print x.strip()
4.
#!/usr/bin/python
x=complex(input('Enter first complex number:'))
y=complex(input('Enter second complex number:'))
print (('The complex number {0} is formed from the real part {0.real} and the imaginary part
{0.imag}.').format(x*y))
5.
#!/usr/bin/python
string1=raw_input('Enter string to reverse:')
print "reversed string is",string1[-1:-(len(string1)+2):-1]