Difference between revisions of "String Operations"
(7 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
+ | <mark>'''Still in edition mode'''</mark> | ||
+ | |||
In Python, strings are a sequence of characters used to store and manipulate text. Python provides many built-in methods to perform common string operations, which are especially useful in data analysis for cleaning, transforming, and analyzing text data. Below are some of the most commonly used string operations. | In Python, strings are a sequence of characters used to store and manipulate text. Python provides many built-in methods to perform common string operations, which are especially useful in data analysis for cleaning, transforming, and analyzing text data. Below are some of the most commonly used string operations. | ||
+ | |||
+ | To apply any of these methods, you have to assign the string to a variable, and then use one of respective methods below next to the variable. For example, imagine that we have a string in a variable called <code>text</code>, you would apply the methods like this: <code>text.lower()</code>, <code>text.len()</code>, <code>text.strip()</code>, etc. In the examples for each method, we will use the function <code>print()</code> to illustrate the output. | ||
== 1. Converting to Lowercase and Uppercase == | == 1. Converting to Lowercase and Uppercase == | ||
Line 17: | Line 21: | ||
To count how many times a specific substring appears in a string, use the count() method. | To count how many times a specific substring appears in a string, use the count() method. | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
− | text = " | + | text = "bananas" |
− | print(text.count("a")) | + | print(text.count("a")) |
</syntaxhighlight> | </syntaxhighlight> | ||
+ | The output will be: | ||
+ | <pre>3</pre> | ||
== 3. Finding the Length of a String == | == 3. Finding the Length of a String == | ||
− | The len() function returns the number of characters in a string. | + | The len() function returns the number of characters in a string, including white spaces. |
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
− | text = "Data" | + | text = "Data Analysis" |
− | print(len(text)) | + | print(len(text)) |
</syntaxhighlight> | </syntaxhighlight> | ||
+ | The output will be: | ||
+ | <pre>13</pre> | ||
== 4. Accessing Characters by Index == | == 4. Accessing Characters by Index == | ||
− | In Python, strings are sequences of characters, and each character in the string has an associated index. The index starts from 0 for the first character and increases by 1 for each subsequent character. | + | In Python, strings are sequences of characters, and each character in the string has an associated index. The index starts from 0 for the first character and increases by 1 for each subsequent character. You can access individual characters in a string using square brackets [], followed by the index of the character you want to access. |
− | You can access individual characters in a string using square brackets [], followed by the index of the character you want to access. | ||
=== Positive Indexing === | === Positive Indexing === | ||
Line 37: | Line 44: | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
text = "Python" | text = "Python" | ||
− | print(text[0]) | + | print(text[0]) |
− | print(text[3]) | + | print(text[3]) |
</syntaxhighlight> | </syntaxhighlight> | ||
+ | The output will be: | ||
+ | <pre> | ||
+ | "P" | ||
+ | "h" | ||
+ | </pre> | ||
In this example, text[0] returns the first character "P", and text[3] returns the fourth character "h". | In this example, text[0] returns the first character "P", and text[3] returns the fourth character "h". | ||
Line 45: | Line 57: | ||
Python also supports negative indexing, where -1 refers to the last character, -2 refers to the second-to-last character, and so on. | Python also supports negative indexing, where -1 refers to the last character, -2 refers to the second-to-last character, and so on. | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
− | text = " | + | text = "Science" |
− | print(text[-1]) | + | print(text[-1]) |
− | print(text[-2]) | + | print(text[-2]) |
</syntaxhighlight> | </syntaxhighlight> | ||
− | In this example, text[-1] returns the last character " | + | The output will be: |
+ | <pre> | ||
+ | "e" | ||
+ | "c" | ||
+ | </pre> | ||
+ | In this example, text[-1] returns the last character "e", and text[-2] returns the second-to-last character "c". | ||
=== IndexError === | === IndexError === | ||
Line 113: | Line 130: | ||
# Output: "My name is Alice and I am 30 years old." | # Output: "My name is Alice and I am 30 years old." | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | |||
+ | <hr> | ||
+ | The author of this entry is Gustavo Rodriguez. AI was used partially in the writing process. | ||
+ | |||
+ | [[Category:Python]] [[Category:Python Basics]] |
Latest revision as of 13:27, 27 September 2024
Still in edition mode
In Python, strings are a sequence of characters used to store and manipulate text. Python provides many built-in methods to perform common string operations, which are especially useful in data analysis for cleaning, transforming, and analyzing text data. Below are some of the most commonly used string operations.
To apply any of these methods, you have to assign the string to a variable, and then use one of respective methods below next to the variable. For example, imagine that we have a string in a variable called text
, you would apply the methods like this: text.lower()
, text.len()
, text.strip()
, etc. In the examples for each method, we will use the function print()
to illustrate the output.
Contents
1. Converting to Lowercase and Uppercase
You can convert a string to all lowercase or uppercase characters using the lower() and upper() methods.
text = "Wiki Methods" print(text.lower()) print(text.upper())
The output will be respectively:
wiki methods WIKI METHODS
2. Counting Occurrences
To count how many times a specific substring appears in a string, use the count() method.
text = "bananas" print(text.count("a"))
The output will be:
3
3. Finding the Length of a String
The len() function returns the number of characters in a string, including white spaces.
text = "Data Analysis" print(len(text))
The output will be:
13
4. Accessing Characters by Index
In Python, strings are sequences of characters, and each character in the string has an associated index. The index starts from 0 for the first character and increases by 1 for each subsequent character. You can access individual characters in a string using square brackets [], followed by the index of the character you want to access.
Positive Indexing
Indexing starts at 0 for the first character, 1 for the second, and so on.
text = "Python" print(text[0]) print(text[3])
The output will be:
"P" "h"
In this example, text[0] returns the first character "P", and text[3] returns the fourth character "h".
Negative Indexing
Python also supports negative indexing, where -1 refers to the last character, -2 refers to the second-to-last character, and so on.
text = "Science" print(text[-1]) print(text[-2])
The output will be:
"e" "c"
In this example, text[-1] returns the last character "e", and text[-2] returns the second-to-last character "c".
IndexError
If you try to access an index that is out of the range of the string, Python will raise an IndexError.
text = "Python" print(text[10]) # Raises IndexError: string index out of range
Slicing Strings
You can access a range of characters by using slicing. The syntax for slicing is start:end, where start is the index to begin slicing (inclusive) and end is the index where slicing stops (exclusive).
text = "Python" print(text[0:2]) # Output: "Py" print(text[2:5]) # Output: "tho"
Slicing also supports negative indexing and allows you to skip characters by providing a step value (start:end:step).
text = "Python" print(text[::2]) # Output: "Pto"
In this example, text[::2] starts at the beginning and takes every second character (P, t, o).
5. Finding Substrings
To find the position of a substring within a string, use the find() method. It returns the index of the first occurrence or -1 if the substring is not found.
text = "data science" print(text.find("science")) # Output: 5 print(text.find("math")) # Output: -1
6. Replacing Substrings
The replace() method replaces occurrences of a substring with another.
text = "I love Python" print(text.replace("Python", "Data Science")) # Output: "I love Data Science"
7. Splitting Strings
The split() method splits a string into a list of substrings based on a specified delimiter (default is space).
text = "apple, banana, cherry" print(text.split(", ")) # Output: ['apple', 'banana', 'cherry']
8. Stripping Whitespace
The strip() method removes leading and trailing whitespace from a string.
text = " Hello World " print(text.strip()) # Output: "Hello World"
9. f-Strings for Formatting
An f string allows you to insert variables directly into a string. It is a modern and efficient way of formatting strings.
name = "Alice" age = 30 print(f"My name is {name} and I am {age} years old.") # Output: "My name is Alice and I am 30 years old."
The author of this entry is Gustavo Rodriguez. AI was used partially in the writing process.