Professional Documents
Culture Documents
Team Emertxe
Strings And Characters
Strings And Characters
Creating Strings
Example-1 s = 'Welcome to Python'
Example-3 s = """
Welcome to Python
"""
Example-4 s = '''
Welcome to Python
'''
n = len(str)
print("Len: ", n)
Strings And Characters
Indexing the Strings
Both positive and Negative indexing is possible in Python
1 str[: :]
Prints all
2 str[0: 9: 1]
Access the string from 0th to 8th element
3 str[0: 9: 2]
Access the string in the step size of 2
4 str[2: 3: 1]
Access the string from 2nd to 3rd Character
5 str[: : 2] Access the entire string in the step size of 2
9 str[-1: -4: -1] When stepsize is negative, then the items are counted from right to
left
10 str[-1: : -1] Retrieve items from str[-1] till the first element from right to left
Strings And Characters
Repeating the Strings
The repetition operator * is used for repeating the strings
print(str * 2)
Example-2
print(str[5: 7] * 2)
Strings And Characters
Concatenation of Strings
+ is used as a concatenation operator
Example-1 s1 = "Core"
s2 = "Python"
s3 = s1 + s2
Strings And Characters
Membership Operator
We can check, if a string or a character is a member of another string or not using
'in' or 'not in' operator
'in' or 'not in' makes case sensitive comaprisons
if sub in str:
print(sub+" is found in main string")
else:
print(sub+" is not found in main string")
Strings And Characters
Removing Spaces
Methods useful for finding the strings in the main string
- find()
- rfind()
- index()
- rindex()
find(), index() will search for the sub-string from the begining
rfind(), rindex() will search for the sub-string from the end
find(): Returns -1, if sub-string is not found
index(): Returns 'ValueError' if the sub-string is not found
Strings And Characters
Finding the Sub-Strings
Syntax mainstring.find(substring, beg, end)
if n == -1:
print("Sub string not found")
else:
print("Sub string found @: ", n + 1)
Strings And Characters
Finding the Sub-Strings
Syntax mainstring.index(substring, beg, end)
n = str.count(‘Delhi’)
n = str.count(‘e’, 0, 3)
n = str.count(‘e’, 0, len(str))
Strings And Characters
Strings are Immutable
Immutable object is an object whose content cannot be changed
Performance Takes less time to allocate the memory for the Immutable objects, since
their memory size is fixed
Security Any attempt to modify the string will lead to the creation of new object in
memory and hence ID changes which can be tracked easily
Strings And Characters
Strings are Immutable
Immutable object is an object whose content cannot be changed
Example:
one two
s1 = “one”
s2 = “two”
S1 S2
one two
S2 = s1
S1 S2
Strings And Characters
Replacing String with another String
print(str1)
Strings And Characters
Splitting And Joining Strings
Syntax stringname.split(‘character’)
lst = str.split(',')
Syntax separator.join(str)
str1 = "-".join(str)
Strings And Characters
Changing the Case of the Strings
Methods upper()
lower()
swapcase()
title()
str = "Python is the future"
Methods startswith()
endswith()
isalpha() Returns True, if the string has atleast one character and all characters are
alphabets(A - Z, a – z)
isdigit() Returns True if the string contains only numeric digits(0-9) and False
otherwise
islower() Returns True if the string contains at least one letter and all characters are
in lower case; otherwise it returns False
isupper() Returns True if the string contains at least one letter and all characters are
in upper case; otherwise it returns False
istitle() Returns True if each word of the string starts with a capital letter and there
at least one character in the string; otherwise it returns False
isspace() Returns True if the string contains only spaces; otherwise, it returns False
Strings And Characters
Formatting the strings
Syntax
"format string with replacement fields". format(values)
id = 10
name = "Ram"
sal = 19000.45
print("{}, {}, {}". format(id, name, sal))
Syntax
"format string with replacement fields". format(values)
n = 5000
print("{:*>15d}". format(num))
print("{:*^15d}". format(num))
Strings And Characters
Exercise
1. To know the type of character entered by the user