Difference between revisions of "String Operations"

From Sustainability Methods
(Created page with "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 especia...")
 
Line 1: Line 1:
 
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.
  
=== 1. Converting to Lowercase and Uppercase ===
+
== 1. Converting to Lowercase and Uppercase ==
 
 
 
You can convert a string to all lowercase or uppercase characters using the lower() and upper() methods.
 
You can convert a string to all lowercase or uppercase characters using the lower() and upper() methods.
 
<syntaxhighlight lang="python">
 
<syntaxhighlight lang="python">
 
text = "Wiki Methods"
 
text = "Wiki Methods"
print(text.lower())  # Output: "hello world"
+
print(text.lower())   
print(text.upper()) # Output: "HELLO WORLD"
+
print(text.upper())
 
</syntaxhighlight>
 
</syntaxhighlight>
 
The output will be respectively:
 
The output will be respectively:
Line 14: Line 13:
 
WIKI METHODS
 
WIKI METHODS
 
</pre>
 
</pre>
 +
 +
== 2. Counting Occurrences ==
 +
To count how many times a specific substring appears in a string, use the count() method.
 +
<syntaxhighlight lang="python">
 +
text = "banana"
 +
print(text.count("a"))  # Output: 3
 +
</syntaxhighlight>
 +
 +
== 3. 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.
 +
<syntaxhighlight lang="python">
 +
text = "Python"
 +
print(text[0])  # Output: "P"
 +
print(text[3])  # Output: "h"
 +
</syntaxhighlight>
 +
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.
 +
<syntaxhighlight lang="python">
 +
text = "Python"
 +
print(text[-1])  # Output: "n"
 +
print(text[-2])  # Output: "o"
 +
</syntaxhighlight>
 +
In this example, text[-1] returns the last character "n", and text[-2] returns the second-to-last character "o".
 +
 +
=== IndexError ===
 +
If you try to access an index that is out of the range of the string, Python will raise an IndexError.
 +
<syntaxhighlight lang="python">
 +
text = "Python"
 +
print(text[10])  # Raises IndexError: string index out of range
 +
</syntaxhighlight>
 +
 +
=== 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).
 +
<syntaxhighlight lang="python">
 +
text = "Python"
 +
print(text[0:2])  # Output: "Py"
 +
print(text[2:5])  # Output: "tho"
 +
</syntaxhighlight>
 +
 +
Slicing also supports negative indexing and allows you to skip characters by providing a step value (start:end:step).
 +
<syntaxhighlight lang="python">
 +
text = "Python"
 +
print(text[::2])  # Output: "Pto"
 +
</syntaxhighlight>
 +
In this example, text[::2] starts at the beginning and takes every second character (P, t, o).

Revision as of 09:28, 18 September 2024

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.

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 = "banana"
print(text.count("a"))  # Output: 3

3. 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])  # Output: "P"
print(text[3])  # Output: "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 = "Python"
print(text[-1])  # Output: "n"
print(text[-2])  # Output: "o"

In this example, text[-1] returns the last character "n", and text[-2] returns the second-to-last character "o".

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).