String Manipulation in Python
String Manipulation
To manipulate strings, we can use some of Pythons built-in methods.
A string is a list of characters in order.
A character is anything you can type on the keyboard in one keystroke,
like a letter, a number, or a backslash.
Strings can have spaces:
"hello world".
An empty string is a string that has 0 characters.
Python strings are immutable
Python recognize as strings everything that is delimited by quotation marks
(” ” or ‘ ‘).
How to Count
Specify what to count as an argument.
In this case, we are counting how many spaces exist in "Rio de Janeiro", which
is 2.
word = "Rio de Janeiro"
print(word.count(' '))
Accessing
Use [ ] to access characters in a string
word = "Hello World"
letter=word[0]
Length
word = "Hello World"
len(word)
String Manipulation in Python
Finding
word = "Hello World">>> print word.count('l') # count how
many times l is in the string
Creation
word = "Hello World"
>>> print word
Hello World
Accessing
Use [ ] to access characters in a string
word = "Hello World"
letter=word[0]
print letter
Length
word = "Hello World"
len(word)
Finding
word = "Hello World">>> print word.count('l') # count how
many times l is in the string
print word.find("H") # find the word H in the string
String Manipulation in Python
print word.index("World") # find the letters World in the
string
Count
s = "Count, the number of spaces"
print s.count(' ')
Slicing
Keep in mind that python, as many other languages, starts to count from 0!!
word = "Hello World"
print word[0] #get one char of the word
print word[0:1] #get one char of the word (same as above)
print word[0:3] #get the first three char
print word[:3] #get the first three char
print word[-3:] #get the last three char
print word[3:] #get all but the three first char
print word[:-3] #get all but the three last character
Split Strings
word = "Hello World"
word.split(' ') # Split on whitespace
['Hello', 'World']
Startswith / Endswith
word = "hello world"
word.startswith("H")
String Manipulation in Python
Replacing
word = "Hello World"
word.replace("Hello", "Goodbye")
'Goodbye World'
Changing Upper and Lower Case Strings
string = "Hello World"
print string.upper()
HELLO WORLD
Reversing
string = "Hello World"
print ' '.join(reversed(string))
d l r o W o l l e H
Concatenation
To concatenate strings in Python use the “+” operator.
"Hello " + "World" # = "Hello World"
"Hello " + "World" + "!"# = "Hello World!"
String Manipulation in Python
Testing
A string in Python can be tested for truth value.
The return type will be in Boolean value (True or False)
word = "Hello World"
word.isalnum() #check if all char are alphanumeric
word.isalpha() #check if all char in the string are
alphabetic
word.isdigit() #test if string contains digits
word.istitle() #test if string contains title words
word.isupper() #test if string contains upper case
word.islower() #test if string contains lower case
word.isspace() #test if string contains spaces
word.endswith('d') #test if string endswith a d
word.startswith('H') #test if string startswith H