String Manipulation
String Manipulation
String Manipulation…Python…
For Computer Science students’
Hopefully, all of you go through the previous uploaded materials. In this material we discuss about “String
Manipulation - Python”
A program to display “Welcome To Python” message to the user combines with his / her name.
Python treats single quotes the same as double quotes i.e. you can use any of them.
But triple quotes are typically used for strings that span multiple lines.
Examples of strings:
‘Python is a programming language’ - single line string single quotes is used
“Python is a very popular language” - single line strings double quotes is used
“ram12”
‘abc_123’
“Kolkata 26”
Creation of strings:
Creation of strings in python is very easy.
e.g.
Empty string:
An empty string is a string without any characters inside.
Note: if a command is long, then it can be shifted to the next line by typing ‘\’, but it shows the result in the same line.
Output:
On the above example backslash ( \ ) is an escape sequence that’s why the output is shown in same line.
If you wish to display the output in the next line, then you use the ‘\n’ escape sequence as follows:
Output:
Multiline strings:
Multiline strings are represented using triple quotes.
Example:
Output:
Traversing a string:
Traversing a string means iterating through the elements of a string, one character at a time.
Output:
You can also write the above code by using while loop as follows:
(Note: to determine the number of iteration you have to use the len( ) function to find the length of the string.)
Before writing the program, first go through the character representation of the above string in memory.
x = “Computer Science”
All of you know that each character in a string has an index value. Indexing starts at 0.
Now, if you want to write the above program by using while loop then you first determine – How many times your loop
will be executed?
As indexing starts at 0 and ends at 15 in the above case, so number of iteration required is 16.
To avoid counting by ourselves we use len( ) function for finding the length. Just look at the following code:
Output:
See, len( ) shows the length is 16. But actually in 16th position is not in the above string. The actual indexing is from 0 to
15.
Output:
Ok?
a = len(x) indicates a = 16
so i < a or you can use i <= a - 1
Output:
Let’s try to write a program that read a string from the user and display it in reverse order.
Output:
if, we write: for i in range(5, 1, -1): it indicates the iterations: 5, 4, 3, 2 ( -1 indicates the steps)
So, in our program, we want to print the string x in reverse order i.e. (from the length) -1 to 0
therefore we use:
step value
for i in range (a -1 , -1, -1)
Let’s try the same program of reverse a string using while loop:-
Output:
String Operators:
Basic Operators:
Example:
Example:
Example:
“a” in “keya” will give True
“a” in “sunil” will give False
Comparison Operators:
Example:
“a” = = “a” will give True
‘Ram’ = = ‘ram’ will give False
‘abcd’ > ‘abcD’ will give True ( as ‘abc’ in both strings are equal but ‘d’ is always greater
than ‘D’
‘string slice’ refers to a part of string, where the string is sliced using a range of indices.
To understand this, we look at the memory representation of a string using its indices again:
x = “amazing world”
Then,
x[ 0 : 7 ] will give “amazing”
x[ 0 : 3 ] will give “ama”
x[ 2 : 5 ] will give “azi”
x[ 4 : 10] will give “ing wo”
syntax: len(string)
Example: Output:
2) capitalize( ) : - this method returns the exact string with the first letter in uppercase.
syntax: string.capitalize( )
Example: Output:
3) find( ) : - returns the lowest index in the string where the substring is found within the slice range of start and stop.
syntax: string.find( substring, start, stop)
Example:
Output:
4) isalnum( ) : - returns True if the character in the string are alphanumeric (alphabets or numbers). False otherwise.
syntax: string.isalnum( )
Example: Output:
5) isalpha( ) : - returns True if all characters in the string are alphabetic. False otherwise.
syntax: string.isalpha( )
Example: Output:
6) isdigit( ) : - returns True if all characters in the string are digits. False otherwise.
syntax: string.isdigit( )
Example: Output:
7) islower( ) : - returns True if all characters in the string are lowercase. False otherwise.
syntax: string.islower( )
Example: Output:
8) isupper( ) : - returns True if all characters in the string are uppercase. False otherwise.
syntax: string.isupper( )
Example: Output:
Example: Output:
Example: Output:
syntax: string.upper( )
Example: Output:
11) lstrip( ) : - returns a copy of the string with leading characters removed.
syntax: string.lstrip( )
Example:
Output:
12) rstrip( ) : - returns a copy of the string with trailing characters removed.
syntax: string.rstrip( )
Example:
Output:
13) split( ) : - breaks up a string at the specified separator and returns a list of substrings.
Example:
Output:
14) replace( ) : - replaces all the occurrences of the old string with the new string.
syntax: string.replace( oldstring, newstring)
Example:
Output:
replaces all occurrences of “is”
When you want to run the above program it shows the following error:
So, you cannot modify the contents of a string after it has been created.
Output:
Program 2) Write a program to count the number of vowels present in a given string.
Solution:
Output:
Program 3) Write a program to count the number of words present in the given string and also display each
word.
Solution:
Output:
Program 4) Write a program to count the number of lowercase and uppercase alphabets present in the given
string.
Solution:
Output:
Program 5) Write a program that takes a string from the user and display the occurrence of words starting with
a vowel in the given string also counts those words.
Solution:
Output:
Program 6) Write a program that takes a word from the user and check that the word is palindrome word or
not.
Example of palindrome word is: MADAM
i.e. the original word and the reverse word is same
Solution: Output:
Program 7) Write a program that reads a string nd display the longest substring of the given string.
Solution:
Output:
Programming Tasks:
1) Write a program that reads a given string and display the number of uppercase letters, number of lowercase letters,
number of alphabets, number of spaces and number of digits are present in the given string.
2) Write a program that reads a given string, counts how many times a substring “is” appears in the given string.
3) Write a program that reads a string from the user then converts each lowercase alphabet to uppercase and all
uppercase alphabets to lowercase. ( Hint: use islower( ), isupper( ), lower( ) and upper( ) function)
4) Write a program that reads a string then prints a string that capitalizes every other letter in the string.
Example: If the given string is: passion
then the resulted string will be: pAsSiOn
5) Write a program that reads a string from the user then prints a string after removing all vowels from the given string.
6) Write a program that reads a string from the user then counts how many words are starting with ‘s’ or ‘S’.
7) Write a program that reads a string from the user then calculates the sum of each digit present in the given string.