0% found this document useful (0 votes)
22 views8 pages

String Operation

This document provides an overview of strings in Python, including their definition, representation, and various operations such as concatenation, replication, and slicing. It also covers string functions, membership and comparison operators, and includes several programming exercises related to string manipulation. Key concepts such as indexing, string length, and character checks are explained with examples.

Uploaded by

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

String Operation

This document provides an overview of strings in Python, including their definition, representation, and various operations such as concatenation, replication, and slicing. It also covers string functions, membership and comparison operators, and includes several programming exercises related to string manipulation. Key concepts such as indexing, string length, and character checks are explained with examples.

Uploaded by

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

CH- 5 STRINGS IN PYTHON

WHAT ARE STRINGS?


Strings are any sequence of characters which are enclosed within a single or double
quotes.
For Example:
a = “Python”
b = “55”
How to represent or Traverse a String ?
a = “Python” Index values
0 1 2 3 4 5
P Y T H O N

a [0] = P
a[1] = Y
a[5] = N
Note: Each character in a string has an index value, and index value will always start
from zero.
String A P Y T H O N
Positive Index 0 1 2 3 4 5
Negative Index -6 -5 -4 -3 -2 -1

For Example:
print(a[-3]) H
String operations:
Concatenation (+) : refers to create a new string by adding 2 strings.
“computer” + “science” = computerscience
“5” + “6” = 56
7 + 8 = 15
“python” + 5 = Error (because str cannot be added to int)
6 + “7” = Error (because string cannot be added to int)

Replication or Repetition ( * ) : creates a new string by multiplying the given string.


2 * “Maths” : Maths Maths
“comp” * 3 : compcompcomp
5 * 5 : 25
“com” * “2” : Error( two string can’t be multiplied )

String Slicing ( : ) It is used to retrieve a subset of values or it is substring of given


string.
Syntax: String_name[start : end]
0 1 2 3 4 5 6
C O M P U T E
-7 -6 -5 -4 -3 -2 -1
print( a[2:6])
output: MPUT
print(a[1:6:2])
OUTPUT: OPT
print( a[ : 3])
OUTPUT: COM
print( a[3:])
OUTPUT: PUTE
print(a[-3:])
OUTPUT: UTE
print( a[ : ] )
OUTPUT: COMPUTE

Membership Operator
1. in : it returns true if character is present in the string.
2. not In: it returns true if character is not present in the string.
Ex:
“H” in “Hello”
True
“G” in “Python”
False
“F” not in “Python”
True
“H” not in “Hello”
False
Comparison Operator: <, >, <= ,>= ,= = , !=
Strings are compared using ASCII value.
ASCII values
A = 65
B =66
a = 97
b= 98
Examples:
“A” < “a”
True
“Bat” > “Boy”
False

String Functions:

# String Fucntions
s = "python"
s1= "563”
print(len(s))
Output: 6
#returns the length of a given string
print(s.capitalize())
Output: Python
#capitalize converts the first letter in uppercase
print(s.upper())
Output:PYTHON
# converts the string into uppercase
print(s.isupper())
Output:False
# it returns the boolean value (true) if the string is
in uppercase
print(s.lower())
Output:python
# converts the string into lower case
print(s.islower())
Output:True
# it returns the boolean value (true) if the string is
in lowercase
print(s1.isdigit()
Output:True
# returns True if all the characters are digits.
print(s1.isalnum())
Output:False
# returns True if all the charcaters are alphanumeric
print(s.isalpha())
Output:True
# returns True if all the characters are aplhabet(a-z)
split(): it breaks the string at the specified seperator,if the seperator is not spe
cified space is used for seperator.
For ex:
s = "informatics;practices;python"
print(s)
print(s.split(“;”))
output:
informatics; practices; python
['informatics', 'practices', 'python']

Q WAP to write the word “hello” letter by letter.


sol: s = "hello"
for i in s:
print(i)
Q WAP to input a string and count the number of vowels.
sol:
s = input("enter string")
c=0
for i in s:
if i in('a','e','i','o','u'):
c=c+1
print("total vowels are " , c)

Q To input a string and count the number of words.


s = input("Enter string")
a = s.split()
c=0
for i in a:
c=c+1
print("total words " , c)

Q To input a sentence and a string which you want to count how many times a
substring appears in the sentence.
For ex: s = Computer is a device computer is hardware component.
s1 = computer
sol:
s = input("enter string")
s1 = input("enter string which u want to search")
a= s.split()
c=0
print(s)
print(a)
for i in a:
if s1==i:
c=c+1
print("total number of times word appears ", c)
Q WAP to input a string and remove vowels from it.
sol:s= input("enter string")
s1=""
for i in s:
if i not in('a','e','i','o','u'):
s1=s1+i
print("after removing vowels " , s1)

Q WAP to input a string and then prints a string that capitalizes every other letter
in the string.
For ex: school output sChOoL
sol:
s= input("enter string")
s1=""
for i in range(len(s)):
if i%2==0:
s1 = s1+s[i]
else:
s1 = s1+s[i].upper()
print("new string ", s1)

Q Consider the string str=”Green Revolution”. Write statements to implement the


following:
a. To display last four characters.
b. To repeat the string 3 times.
c.To display the starting index for the substring ‘vo’.
d. To check whether the string contains ‘vol’ or not.

You might also like