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

String Functions Class11 2

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

String Functions Class11 2

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

String manipulation

• String :
Characters enclosed in quotes of any types.(single/double/triple)
• Empty string - ‘ ’
• Strings are immutable
• Each characters in a string can be accessed using index.
• Forward indexing (0 to length-1)
• Backward indexing (-1 to –length)
• Traversing a string:
Iterate through the elements of a string, one character at a time.
For loop can be used for the string traversal.
1st method:
Eg:
name = “Superb”
for ch in name:
print(ch, end = “ - ”)

OUTPUT:
S-u-p-e-r-b-
• 2nd method:

name = “Superb”
for ch in range(len(name)):
print(name[ch], end = “ - ”)

OUTPUT:
S-u-p-e-r-b-
Q. Write a program to print the reverse of a string.
• String operators:
• Basic operators:
1. + concatenation operator
2. * replication operator

Concatenation operator:
It is used for joining two strings.
Both operands should be string.
Eg: “APP” + “LE” gives “APPLE”
“3” + “2” gives “32”
“3” + 2 gives error, since 2 is int type.
3+2 ?
• Replication operator:
• Replicates a string, the mentioned number of times.
• Number * string OR string * number
• 3 * “go” results “ gogogo”
• Operands – one should be number and other string.
•3 * 3 = 9
• “go” * “go” results in error.
• 5 * “ *” results in “*****”
• 5 * “5” results in “55555”
Q. Print the following without using nested loop
#
##
## #
# # # #
• Membership operator: in , not in
• Eg: “a” in “abc” returns True, since a is a member in that string.
“a” not in “abc” returns False.
“ad” in “abc” returns False
“A” in “abc” returns False
d in “abc” returns False
d not in “abc” returns True

Eg: string 1 = “Jap”


string2 = “Japan”
Here string1 is a substring of string2.
Q. Program to accept 2 strings and check if one is a substring of
another.

Q. Write a program to accept an alphanumeric string and return the


count of alphabets and numbers in the string.
• Comparison operator:
All the relational operators can be used to compare string also.
The comparisons are based on the ASCII or UNICODE values.
Uppercase letters have smaller ASCII values than smallercase letters.

“a” == “a” returns True


“a” == “A” returns False
“a” > “A” returns True, since it compares 97 > 65.
“abc” > “abC”, returns True, character by character comparison occurs.
To get the UNICODE value of a character use ord(‘<character>’)
Eg: ord(‘A’) gives 65.
• To get the character corresponding to a UNICODE value use chr()
Eg: chr(65) gives ‘A’.

Q. Write a program to check if a user name and password entered by the


user is correct or not.
Let user = “user1”
password = “1234abcd”

Then ask the user to enter a username and password and compare those
values with user and password.

You might also like