String Functions Class11 2
String Functions Class11 2
• 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
Then ask the user to enter a username and password and compare those
values with user and password.