Module 4: Strings in Python
- Use the most common string helper functions.
- Implement a variety of formatting techniques.
String basics in Python
fact = "The Moon has no atmosphere."
print(fact)
Strings and string variables are immutable
fact = "The Moon has no atmosphere."
fact + "No sound can be heard on the Moon."
print(fact)
# Output: "The Moon has no atmosphere."
Operations on strings always produce new strings as a result.
fact = "The Moon has no atmosphere."
two_facts = fact + "No sound can be heard on the Moon."
print(two_facts)
# Output: The Moon has no atmosphere.No sound can be heard on the Moon.
Using Quotation marks - be consistent
'The "near side" is the part of the Moon that faces the Earth.'
# or
"We only see about 60% of the Moon's surface."
# but don't do this:
'We only see about 60% of the Moon's surface.'
# sometimes you need to use 3 quotation marks
"""We only see about 60% of the Moon's surface, this is known as the "near side"."""
Multiline text:
- Use a newline character (\n).
- Use triple quotation marks (""").
multiline = "Facts about the Moon:\n There is no atmosphere.\n There is no sound."
print(multiline)
# or
multiline = """Facts about the Moon:
There is no atmosphere.
There is no sound."""
print(multiline)
String Methods in Python
print("temperatures and facts about the moon".title())
# Output: Temperatures And Facts About The Moon
# or assigning to a variable
heading = "temperatures and facts about the moon"
heading_upper = heading.title()
print(heading_upper)
# Output: Temperatures And Facts About The Moon
Splitting:
temperatures = "Daylight: 260 F Nighttime: -280 F"
temperatures_list = temperatures.split()
print(temperatures_list)
# Output: ['Daylight:', '260', 'F', 'Nighttime:', '-280', 'F']
# or
temperatures = "Daylight: 260 F\n Nighttime: -280 F"
temperatures_list = temperatures.split('\n')
print(temperatures_list)
# Output: ['Daylight: 260 F', 'Nighttime: -280 F']
Joining
You can join strings together using the .join() method
moon_facts = ["The Moon is drifting away from the Earth.", "On average, the Moon is moving about 4cm every year."]
print(' '.join(moon_facts))
# Output: The Moon is drifting away from the Earth. On average, the Moon is moving about 4cm every year.
Search for a string
in method
You can search directly by using the in
operator
print("Moon" in "This text will describe facts and challenges with space travel")
# Output: False
print("Moon" in "This text will describe facts about the Moon")
# Output: True
Find method
temperatures = """Saturn has a daytime temperature of -170 degrees Celsius, while Mars has -28 Celsius."""
print(temperatures.find("Moon"))
# Output: -1 is used when you can't find the term in the string.
temperatures = """Saturn has a daytime temperature of -170 degrees Celsius, while Mars has -28 Celsius."""
print(temperatures.find("Mars"))
# Output: 64 - the # of characters into the string it is.
Count method
temperatures = """Saturn has a daytime temperature of -170 degrees Celsius, while Mars has -28 Celsius."""
print(temperatures.count("Mars")) # Output: 1
print(temperatures.count("Moon")) # Output: 0
Upper and lower methods
Strings in Python are case sensitive, meaning moon and Moon are not the same string.
print("The Moon And The Earth".lower())
# Output: the moon and the earth
print("The Moon And The Earth".upper())
# Output: THE MOON AND THE EARTH
Note: Lowercase a string so that casing doesn't prevent a match. For example, if you're counting the number of times the word the appears, the method wouldn't count the times where The appears, even though they're both the same word. You can use the .lower() method to change all characters to lowercase.
Check content
temperatures = "Mars Average Temperature: -60 C"
parts = temperatures.split(':')
print(parts)
print(parts[-1])
# Output:
# ['Mars average temperature', ' -60 C']
#' -60 C'
You can only do this if the text is somewhat regular. If it's not, consider the following:
mars_temperature = "The highest temperature on Mars is about 30 C"
for item in mars_temperature.split():
if item.isnumeric():
print(item)
# Output: 30
Like the .isnumeric() method, .isdecimal() can check for strings that look like decimals. NOTE: I"-70".isnumeric() returns False. This is because all characters in the string would need to be numeric, and the dash (-) isn't numeric. The .isnumeric() method doesn't work with negative numbers unless you work around it."
Examples:
print("-60".startswith('-'))
# Output: True
if "30 C".endswith("C"):
print("This temperature is in Celsius")
# Output: This temperature is in Celsius
Transform Text
You can use .replace() to replace text
print("Saturn has a daytime temperature of -170 degrees Celsius, while Mars has -28 Celsius.".replace("Celsius", "C"))
# Output: Saturn has a daytime temperature of -170 degrees C, while Mars has -28 C.
Lower is a fantastic way to normalize text to do some sort of insensitive search
text = "Temperatures on the Moon can vary wildly."
print("temperatures" in text)
# output: False
# but...
text = "Temperatures on the Moon can vary wildly."
print("temperatures" in text.lower())
# Output: True
## Exercise: Transforming strings
---
1. Create a variable and store a paragraph
```python
text = """Interesting facts about the Moon. The Moon is Earth's only satellite. There are several interesting facts about the Moon and how it affects life here on Earth. On average, the Moon moves 4cm away from the Earth every year. This yearly drift is not significant enough to cause immediate effects on Earth. The highest daylight temperature of the Moon is 127 C."""
- Split the paragraph into sentences:
sentences = text.split('. ')
print(sentences)
- Search for a term
for sentence in sentences:
if 'temperature' in sentence:
print(sentence)
- The output should be:
The highest daylight temperature of the Moon is 127 C.
String format in Python
Take the following text. The %s is the placeholder. You can then send in the placeholder and the string after, such like this.
mass_percentage = "1/6"
print("On the Moon, you would weigh about %s of your weight on Earth." % mass_percentage)
Multiple values uses a bit of a different syntax:
print("""Both sides of the %s get the same amount of sunlight, but only one side is seen from %s because the %s rotates around its own axis when it orbits %s.""" % ("Moon", "Earth", "Moon", "Earth"))
You can use this method of substitution, but one of the next methods are better preferred.