python course 2 week 1 Index of Strings Get the Length of a String Looping through Strings Slicing Strings String Concatenation Check if one string ‘is’ in another string Searching a String Make Uppercase Search and Replace Stripping Whitespace Prefixes

String manipulation in Python 3.

Use input() function
The input data is string, needed to be converted to other data types if necessary, for example, num = int(str)

1
2
3
4
name = input('Enter a name: ')
print(name)

# Tracy

Index of Strings

1
2
3
4
5
fruit = 'banana'
print(fruit[0]) # output: b. Pronouncation: fruit sub 0
x = 3
w = fruit[x - 1]
print(w) # output: n

Get the Length of a String

len() function

1
print(len(fruit)) # output: 6

Looping through Strings

1
2
3
4
5
6
7
8
9
10
11
# while loop
index = 0
while index < len(fruit):
letter = fruit[index]
print(index, letter)
index += 1
print('Done')
# for loop
for letter in fruit:
print(letter)
print('Done')

Slicing Strings

  1. Use the colon operator, ‘start’:’end’
  2. The first number is including, the second number is excluding.
  3. If the second number is beyond the end of this string, it gets the rest of the string.

    1
    2
    3
    s = 'Tracy Yang'
    print(s[0:4]) # output: Trac
    print(s[4:20]) # output: y Yang
  4. No first number: get the substring from 0 (including) to the second number (excluding)

  5. No second number: get the substring from the first number (including) to the end
  6. Just colon operator: get the whole string
1
2
3
print(s[:2]) #Tr
print(s[8:]) #ng
print(s[:]) #Tracy Yang

String Concatenation

1
2
3
4
5
a = 'Hello'
b = a + 'There'
print(b) # output: HelloThere
c = a + ' ' + 'There'
print(c) # output: Hello There

Check if one string ‘is’ in another string

1
2
3
print('n' in fruit) #True
print('m' in fruit) #False
print('nan' in fruit) #True

Searching a String

string.find():

  1. Returns the index of the first occurrence
  2. Returns -1 if not found
1
2
3
4
5
6
na = fruit.find('na')
# at index of 2
print(na) # output: 2
ma = fruit.find('ma')
# not exist
print(ma) # output: -1

Make Uppercase

string.upper(): returns a new string, the old one doesn’t change.

1
2
greet = 'Hello Tracy'
print(greet.upper()) # output: HELLO TRACY

Search and Replace

string.replace()

  1. Replace a string with another, the old string doesn’t change.
  2. It replaces all occurrence in the old string.
  3. If the target substring is not found, then the original string is returned.
1
2
3
4
5
6
7
8
9
10
# 1.
greet2 = greet.replace('Tracy', 'Lucy')
print(greet2) # output: Hello Lucy
print(greet) # output: Hello Tracy
# 2.
greet3 = greet.replace('l', '!')
print(greet3) # output: He!!o Tracy
# 3.
greet4 = greet.replace('X', 'a')
print(greet4, 123) # output: Hello Tracy 123

Stripping Whitespace

  1. string.lstrip() and string.rstrip(): remove whitespace at left or right
  2. string.strip(): remove whitespace at both sides
1
2
3
4
whiteSpace = '    Hello There   '
print(whiteSpace.lstrip()) # output: 'Hello There '
print(whiteSpace.rstrip()) # output: ' Hello There'
print(whiteSpace.strip()) # output: 'Hello There;

Prefixes

string.startswith(str): return True if the string starts with the input, otherwise False.

1
2
3
line = 'Please sit down.'
print(line.startswith('Please')) # output: True
print(line.startswith('p')) # output: False