0% found this document useful (0 votes)
27 views1 page

Answers:: Operations ON String Variables

This document discusses string operations in Python and provides examples of programs to: 1) compare two strings without considering case, 2) find the number of occurrences of a substring, 3) trim leading and trailing spaces from a string, 4) add and multiply two complex numbers and display the output and real/imaginary parts separately, and 5) reverse a string without using the reversed() function. Sample Python code is given for each operation.

Uploaded by

Suyash Sarvate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views1 page

Answers:: Operations ON String Variables

This document discusses string operations in Python and provides examples of programs to: 1) compare two strings without considering case, 2) find the number of occurrences of a substring, 3) trim leading and trailing spaces from a string, 4) add and multiply two complex numbers and display the output and real/imaginary parts separately, and 5) reverse a string without using the reversed() function. Sample Python code is given for each operation.

Uploaded by

Suyash Sarvate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Chapter 8:

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.

5. Write a program to reverse a string without using reversed() inbuilt function.

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]

You might also like