Assignment 1 String
Assignment 1 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().
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.
output: hsay
To Check palindrome:
name = "ullu"
Str=””
length= len (name) - 1
Substring
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.