STRING MANIPULATION
[email protected] 0777634287/0712389336
Chapter 3, Slide 1 Starting Out with Visual Basic 3rd Edition
Len Function
• Displays the length of the string
string = “Awesome Python”
print (len(string))
Output:
14
[email protected] 0777634287/0712389336
Chapter 3, Slide 2 Starting Out with Visual Basic 3rd Edition
Str Function
• Converts a number to a string
string = “I am ”
age = 30
print (string + str(age)) or print (string + ‘age’)
Output:
I am 30
NB: str or ‘ ‘ convert value 18 to string.
[email protected] 0777634287/0712389336
Chapter 3, Slide 3 Starting Out with Visual Basic 3rd Edition
Example 1
string = “Awesome Python”
print (string(0:6))
Output:
Awesome
NB: 0 indicates the first character A and 6 is
the seventh character e.
[email protected] 0777634287/0712389336
Chapter 3, Slide 4 Starting Out with Visual Basic 3rd Edition
Str.capitalise
• Capitalize the first letter in a string
string = “i am feeling good.”
print (string.capitalize())
Output:
I am feeling good.
[email protected] 0777634287/0712389336
Chapter 3, Slide 5 Starting Out with Visual Basic 3rd Edition
Str.lower
• Capitalize the first letter in a string
string = “ENJOY PYTHON.”
print (string.lower())
Output:
enjoy python.
[email protected] 0777634287/0712389336
Chapter 3, Slide 6 Starting Out with Visual Basic 3rd Edition
Str.count
• Count the number of occurrence of a given
word in a string
string = “i am feeling good very goog.”
print (string.count(‘good’))
Output:
2
[email protected] 0777634287/0712389336
Chapter 3, Slide 7 Starting Out with Visual Basic 3rd Edition
Str.endswith
• Checks whether a string ends with a given
word
string = “ www.mutareteachers.ac.zw”
print (string.endswith(‘.zw’))
Output:
True
[email protected] 0777634287/0712389336
Chapter 3, Slide 8 Starting Out with Visual Basic 3rd Edition
Str.find
• Finds the position of a given word in a string
string = “I enjoy computers.”
print (string.find(‘enjoy’))
Output:
2 :Exists at position 2. Starts counting at 0
string = “I enjoy computers.”
print (string.find(‘you’))
Output:
-1 :You does not exist in the string
[email protected] 0777634287/0712389336
Chapter 3, Slide 9 Starting Out with Visual Basic 3rd Edition
Str.islower
• Finds whether the string is lower case
string = “I Enjoy Computers.”
print (string.islower())
Output:
False : I, E and C are all uppercase.
[email protected] 0777634287/0712389336
Chapter 3, Slide 10 Starting Out with Visual Basic 3rd Edition
Str.isupper
• Finds whether the string is upper case
string = “ENJOY PYTHON.”
print (string.isupper())
Output:
True
[email protected] 0777634287/0712389336
Chapter 3, Slide 11 Starting Out with Visual Basic 3rd Edition
Str.strip
• Removes certain given characters in a string
string = “!!!!Hie! How are you!!!!?”
print (string.strip(‘!’))
Output:
“Hie How are you?”
[email protected] 0777634287/0712389336
Chapter 3, Slide 12 Starting Out with Visual Basic 3rd Edition
Str.Istrip
• Removes certain left characters in a string
string = “ !!!!!How are you?”
print (string.Istrip(‘!’))
Output:
“How are you?”
[email protected] 0777634287/0712389336
Chapter 3, Slide 13 Starting Out with Visual Basic 3rd Edition
Str.rstrip
• Removes certain right characters in a string
string = “Hie! How are you!!!!?”
print (string.Istrip(‘!’))
Output:
“Hie! How are you?”
[email protected] 0777634287/0712389336
Chapter 3, Slide 14 Starting Out with Visual Basic 3rd Edition
Str.replace
• Replaces certain word(s) in a string
string = “Hie! How are you!!!!?”
print (string.replace (‘Hie’, ‘Hello’))
NB: Replaces Hie with Hello
Output:
“Hello! How are you?”
[email protected] 0777634287/0712389336
Chapter 3, Slide 15 Starting Out with Visual Basic 3rd Edition
Str.split
• Separates a string into two or three
depending on whether there are spaces
between words.
string = “Lovemore Manyeruke?”
print string.split()
Output:
Lovemore Manyeruke
[email protected] 0777634287/0712389336
Chapter 3, Slide 16 Starting Out with Visual Basic 3rd Edition
Str.split cont’d
• Also used to remove certain parts of an original
string.
• For example, let’s remove the letter a from the
string:
balloon = "Sammy has a balloon.”
print string.split(‘a’)
Output:
'S', 'mmy h', 's ', ' b', 'lloon.'
[email protected] 0777634287/0712389336
Chapter 3, Slide 17 Starting Out with Visual Basic 3rd Edition
Str.swapcase
• Changes lower to uppercase and uppercase
to lowercase.
string = “I love PROGRAMMING”
print string.swapcase()
Output:
i LOVE programming
[email protected] 0777634287/0712389336
Chapter 3, Slide 18 Starting Out with Visual Basic 3rd Edition
Str.title
• Changes text in a string to title case.
string = “I love PROGRAMMING”
print string.title()
Output:
I Love Programming
[email protected] 0777634287/0712389336
Chapter 3, Slide 19 Starting Out with Visual Basic 3rd Edition
Other String Methods
• string.islower()
• string.isupper
• string.isspace
• string.isnumeric
• string.isalnum (is alphanumeric)
• string.isalpha (is alphabetic)
• string.istitle
[email protected] 0777634287/0712389336
Chapter 3, Slide 20 Starting Out with Visual Basic 3rd Edition
Example
number = 5
letters = "abcdef“
print(number.isnumeric())
print(letters.isnumeric())
Output
True
False
[email protected] 0777634287/0712389336
Chapter 3, Slide 21 Starting Out with Visual Basic 3rd Edition