0% found this document useful (0 votes)
477 views

Assignment 1 String

The document discusses string operations in Python like finding the longest word, character frequency, checking for palindromes, finding substrings, and counting word occurrences. It provides examples of using built-in string methods like len(), lower(), upper(), replace(), and slicing to perform operations on strings. The assignment asks students to write a Python program to implement several string manipulation tasks and demonstrate understanding of string operations in Python.

Uploaded by

tushar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
477 views

Assignment 1 String

The document discusses string operations in Python like finding the longest word, character frequency, checking for palindromes, finding substrings, and counting word occurrences. It provides examples of using built-in string methods like len(), lower(), upper(), replace(), and slicing to perform operations on strings. The assignment asks students to write a Python program to implement several string manipulation tasks and demonstrate understanding of string operations in Python.

Uploaded by

tushar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment 1

Aim: Write a python program to compute following operations on String:


a) To display word with the longest length
b) To determines the frequency of occurrence of particular character in the string
c) To check whether given string is palindrome or not
d) To display index of first appearance of the substring
e) To count the occurrences of each word in a given string

Pre-requisite:
Basics of String operations

Objectives: 1) To understand the use standard library functions for string operations.
2) To perform the string operations.
Theory:

String:
• Collection of one or more character
• String can be put in ( ‘ ‘),(“ ”) quotes
• each character in the string has particular index through which you can access that
character.
• strings are immutable
• first character of string is access with index 0 accessed and nth character (n-1)
index.
• you can access the substring via slicing along with index
• you can makes changes or you can modify string with methods such as replace(),
join()or split().

eg: str = “Prasad”


print (str)
print(str[1])
print(type(str))
Prasad
r
<class ‘str’>
Also we can calculate length of string with len() operator
print (len (str))
11
different methods such as lower()
upper()
replace()
make coding easy and effective.

String representation in python


It is a sequence of characters.
string are generally enclosed in single double or triple quotes
you can make variable representation of string

eg: Str= " Hello world”


print (str)
Hello world.

or we can put string directly print function that is another representation of


string.
eg. print ("Hi")

Hi

we can use different method (inbuilt) which are string used to modify improve
its readability
eg. Lower(), replace (),
String operations:
length: len() method is used to compute the length of a string
eg: Str= " Hello world”
print (len (str))
11
It counts no. of character in each that the string and returns output as count of
characters.
Copy(): replace method in python can be used to copy one string to another
eg: str= “Yash dilip Patil"
name = str. replace (str, str)
print (name)
Yash dilip Patil

this method will erase of str and replace it with Str and that new string will be
stored in name.
or
we can apply for loop through the length of str. and go on appending each
character into new string
str =" yash dilip Patil”
for i in str:
name = name + i
print(name)
yash dilip Patil
Compare :
When we compare two string there characters with respect a particular index
in whole string must be same.

It uses (= =) operator to compare two strings


Eg:
Str1= “Yash”
Str2 = " Yash "
if (Str1 == Str2):
print (" Both strings are same ")
else:
print (" These are different strings”)

→ Both strings are same

(4) reverse, palindrome


To get reverse
name= "yash”

length = len (name) - 1.


str= “ ”
while (length> 0):
str= str + name [length]
length-=1
print(str)

output: hsay
To Check palindrome:

name = "ullu"
Str=””
length= len (name) - 1

while ( length >=0):


Str= str+ name [ length]
length-=1
if(name == str):
print (" given string is palindrome")
else:
print (" given string is not palindrome" )
Output: given string is palindrome.

Substring

To get the substring: index

Main string = " My name is yash"


Substring= “name”
for i in main string :
For j in range( len (Substring)):
If (i == substring [j]):
count +=1
if (count: len [ substring)):
print ("Index of substring is,", i)
else:
continue
else:
break

Conclusion: Hence, we have performed string operations successfully.

Questions:
1. Write a program to find the length of string.
2. Write a program to display string from backward.
3. Write a program to count number of words in string.
4. Write a program to concatenate one string contents to another.
5. Write a program to compare two strings they are exact equal or not.
6. Write a program to check a string is palindrome or not.
7. WAP to find a substring within a string. If found display its starting position.
8. Write a program to reverse a string.
9. Write a program to convert a string in lowercase.
10. Write a program to convert a string in uppercase.

You might also like