Strings seem like a pretty basic data type in Python (and many other languages, as well). However, it's important to remember that strings are very common – in fact, much of the data that we work with starts out as a string, whether we gather data directly from user input or read it from a file. Another thing that is very important to remember about strings is that they are a very flexible, forgiving data type, and that gives us all plenty of room to do some really interesting and powerful things.
Using the built-in string methods in Python, and a little bit of brain power, we can treat strings like puzzles and get them to do all sorts of interesting things for us. We can make sense of the things that strings are telling us, and even convert them to more meaningful types of data, like numeric types and lists, which enable us to do even more interesting things with them.
In this section, we will learn how to do all kinds of magic with strings. This knowledge will be very useful to you as you work to develop more sophisticated programs over time.
String concatenation is the process of combining two or more strings into a single string. In Python, there are multiple ways to perform string concatenation:
The + operator can be used to concatenate strings together. It combines the contents of two strings and returns a new string that contains both.
str1 = "Hello"
str2 = "World"
result = str1 + ", " + str2
print(result) # Output: Hello, World
The += operator provides an in-place concatenation operation. It appends the second string to the end of the first string and updates the original string.
str1 = "Hello"
str2 = "World"
str1 += ", " + str2
print(str1) # Output: Hello, World
The join() method is used to concatenate strings from an iterable. It takes an iterable (e.g., a list) as an argument and joins the elements together using the specified string as a separator.
words = ["Hello", "World"]
result = " ".join(words)
print(result) # Output: Hello World
String concatenation is useful when you need to combine strings to form more complex or meaningful outputs. It allows you to build dynamic messages, construct file paths, generate SQL queries, and more.
However, it's important to note that excessive concatenation of large strings can be inefficient due to the immutability of strings in Python. In such cases, it's recommended to use other techniques like using a list and join() for improved performance.
In Python, string slicing is a technique that allows you to extract specific portions or substrings from a string. It is done by specifying the indices or positions of the characters you want to extract using the slicing syntax [start:end:step]. Here's a breakdown of each component:
Now, let's see some examples to illustrate string slicing:
text = "Hello, World!"
# EXAMPLE 1: Extract a substring starting from index 7 to the end
substring1 = text[7:]
print(substring1) # Output: World!
# EXAMPLE 2: Extract a substring from index 0 to index 5 (exclusive)
substring2 = text[0:5]
print(substring2) # Output: Hello
# EXAMPLE 3: Extract a substring from index 7 to index 12 (exclusive)
substring3 = text[7:12]
print(substring3) # Output: World
# EXAMPLE 4: Reverse the string
reversed_text = text[::-1]
print(reversed_text) # Output: !dlroW ,olleH
# EXAMPLE 5: Extract a substring in reverse order
substring4 = text[12:5:-1]
print(substring4) # Output: dlroW
In EXAMPLE 1, the substring starts from index 7 (character 'W') until the end of the string. By omitting the end index, the slice includes all characters from the start index to the end.
In EXAMPLE 2, the substring starts from index 0 (character 'H') and ends at index 5 (character ','), but the character at the end index is not included in the slice.
In EXAMPLE 3, the substring starts from index 7 (character 'W') and ends at index 12 (character '!'), excluding the character at the end index.
In EXAMPLE 4, the string is reversed by using a negative step value of -1. This allows us to traverse the string in reverse order, resulting in the reversed text.
In EXAMPLE 5, a substring in reverse order is extracted. It starts from index 12 (character '!'), ends at index 5 (character ','), and the step value of -1 ensures that the characters are included in reverse order.
By leveraging string slicing, you can easily extract specific portions of a string, reverse strings, or create substrings in Python.
In Python, the len() function is used to find the length of a string. It returns the number of characters present in the string, including whitespace and special characters. Here's an explanation with examples:
To find the length of a string, you can simply pass the string as an argument to the len() function. The function will return an integer representing the length of the string.
Here's an example:
text = "Hello, World!"
length = len(text)
print(length) # Output: 13
In this example, the string text contains the phrase "Hello, World!". By calling len(text), we obtain the length of the string, which is 13 characters.
The len() function is particularly useful when you need to validate the input length or perform operations that depend on the size of the string. For instance, you can use it to check if a string meets a specific length requirement or iterate over the characters of a string using a loop.
username = "john_doe"
if len(username) <= 10:
print("Username is valid.")
else:
print("Username is too long.")
# Output: Username is valid.
In this example, we check if the length of the username string is less than or equal to 10 characters. If it is, we print a message indicating that the username is valid; otherwise, we print a message stating that the username is too long.
Remember that the len() function works not only with strings but also with other iterable objects like lists, tuples, and dictionaries, where it returns the number of elements in the object.
Using len() allows you to easily determine the length of a string, enabling you to make decisions, validate input, or perform any other operation that requires knowledge of the string's size.
the replace() method is used to replace occurrences of a specified substring within a string with a new substring. It allows you to modify parts of a string by providing the substring to be replaced and the substring to replace it with.
The syntax for using the replace() method is as follows:
new_string = original_string.replace(old_substring, new_substring)
Here, original_string is the string you want to modify, old_substring is the substring you want to replace, and new_substring is the substring that will replace the occurrences of old_substring.
Here's an example that demonstrates the usage of replace():
text = "Hello, World!"
new_text = text.replace("World", "Universe")
print(new_text) # Output: Hello, Universe!
In this example, we have a string text containing the phrase "Hello, World!". By using the replace() method, we replace the substring "World" with "Universe". The modified string is stored in the new_text variable. When we print new_text, we get the output "Hello, Universe!".
It's important to note that the replace() method returns a new string with the replacements and does not modify the original string. Strings in Python are immutable, meaning they cannot be changed in-place. Therefore, the replace() method creates a new string with the desired modifications.
Additionally, if the old_substring is not found in the original string, the replace() method will not make any changes and return the original string as it is.
text = "Hello, World!"
new_text = text.replace("Universe", "Python")
print(new_text) # Output: Hello, World! (No changes made)
In this example, since "Universe" is not found in the original string, the replace() method does not make any modifications, and the output remains the same as the original string "Hello, World!".
By using the replace() method, you can easily update specific parts of a string in Python. It provides a convenient way to perform substitutions and modifications within strings.
In Python, the strip(), lstrip(), and rstrip() methods are used to remove whitespace characters from a string. Here's an explanation of each method and examples to illustrate their usage:
The strip() method removes leading and trailing whitespace characters from a string.
It returns a new string with the whitespace removed.
If no argument is provided, it removes all whitespace characters (spaces, tabs, and newlines).
text = " Hello, World! "
stripped_text = text.strip()
print(stripped_text) # Output: “Hello, World!”
The lstrip() method removes leading (left) whitespace characters from a string.
It returns a new string with the leading whitespace removed.
If no argument is provided, it removes leading spaces, tabs, and newlines.
text = " Hello, World! "
left_stripped_text = text.lstrip()
print(left_stripped_text) # Output: “Hello, World! ”
The rstrip() method removes trailing (right) whitespace characters from a string.
It returns a new string with the trailing whitespace removed.
If no argument is provided, it removes trailing spaces, tabs, and newlines.
text = " Hello, World! "
right_stripped_text = text.rstrip()
print(right_stripped_text) # Output: “ Hello, World!”
The strip(), lstrip(), and rstrip() methods also accept an optional argument, which specifies the characters to be removed instead of just whitespace. By providing a string of characters as an argument, those specific characters will be stripped from the string. For example:
text = "!!!Hello, World!!!"
stripped_text = text.strip("!")
print(stripped_text) # Output: “Hello, World”
In the above example, the exclamation marks (!) are removed from both ends of the string. You can customize the characters to be removed according to your specific requirements.
These methods are particularly useful when dealing with user input, reading data from files, or cleaning up strings before further processing. By removing leading or trailing whitespace, you can ensure that your strings are formatted correctly and ready for use.
The find() and index() methods are used to search for a substring within a string. Here's an explanation of each method, along with examples and recommendations for their use cases:
The find() method searches for the first occurrence of a substring within a string and returns the index of the substring if found. If the substring is not found, it returns -1.
Syntax: string.find(substring, start, end)
Use Case: Use find() when you want to check if a substring exists in a string and determine its position without raising an exception.
Example:
text = "Hello, World!"
index = text.find("World")
print(index) # Output: 7
index = text.find("Python")
print(index) # Output: -1 (substring not found)
The index() method works similarly to find(), but it raises a ValueError if the substring is not found instead of returning -1.
Syntax: string.index(substring, start, end)
Use Case: Use index() when you expect the substring to be present in the string and want to know its position. It can help identify if a substring is missing or verify the correctness of the data.
Example:
text = "Hello, World!"
index = text.index("World")
print(index) # Output: 7
index = text.index("Python") # Raises ValueError
When choosing between find() and index(), consider the following recommendations:
The split() method is used to split a string into a list of substrings based on a specified delimiter. The split() method is called on a string and takes an optional delimiter argument. Here's an explanation of how to use the split() method with examples:
Syntax:
string.split(delimiter)
text = "Hello, World!"
words = text.split()
print(words) # Output: ['Hello,', 'World!']
In this example, the split() method is called without a delimiter. It splits the string text at each whitespace, resulting in a list of substrings ['Hello,', 'World!']. By default, the whitespace characters (spaces, tabs, and newlines) are used as delimiters.
numbers = "1,2,3,4,5"
number_list = numbers.split(",")
print(number_list) # Output: ['1', '2', '3', '4', '5']
In this example, the split() method is called with a comma , as the delimiter. It splits the string numbers at each comma, resulting in a list of substrings ['1', '2', '3', '4', '5']. The comma is used as the delimiter to separate individual numbers.
Using the split() method effectively allows you to break down a string into meaningful substrings based on specific delimiters, facilitating further processing or analysis of the data within the string.
No terms have been published for this module.
Test your knowledge of this module by choosing options below. You can keep trying until you get the right answer.
Skip to the Next QuestionDownload the “lincoln_swift.py” file
In this file, you will find a couple of quotes by people you might recognize – Abraham Lincoln and Taylor Swift. They are the words of Lincoln’s Second Inaugural Address, which is written on the wall of the Lincoln Memorial, and the lyrics for Taylor Swift’s hit song, Antihero.
We are going to use some text analysis techniques to try to analyze which of these two pieces of writing demonstrates the higher level of intelligence. Now, there are many ways to judge the intelligence of a piece of writing, and none of them is really right or wrong, or even the “best” way. Is it the length of the writing? The number of words? What about average word length, or word variety?
The code file contains comments that should help guide your exploration of this text. By comparing the two pieces of writing across several different measures, maybe we can form some kind of opinion (although maybe we already have one).
The instructions in the code file itself will help you get started on how this sandbox challenge can work. However, I’m hoping you will think of some of your own ideas and see what you can come up with. Post the output of your code as a screenshot, as well as your interpretation of what it might mean.
In this assignment, I will provide you with. a few test strings, which will contain random words and phrases, as well as email addresses. Your challenge will be to use the advanced string functions learned in Module 8 to extract all of the email addresses from the text, and put them into an array, then print them out using a for loop, after removing duplicates. You will also have a couple of sample email addresses that you will scan for in the email addresses you pull out of the text.