String Processing

String processing refers to the operations performed on strings that allow them to be accessed, analyzed, and updated.

String Traversal

The characters in a string can be easily traversed, without the use of an explicit index variable, using the for chr in string form of the for statement

text = "I have a pet cat and a pet dog."
count_a = 0
for char in text:
    if char == 'a': 
        count_a += 1  
print("The number of 'a' letters is:", count_a) #The number of 'a' letters is: 4

String-Applicable Sequence Operations

Strings are immutable, so sequence-modifying operations return a new string that is a modified version of the original. Some sequence operations include:

Example operations for s = 'Hello Goodbye!':

String Methods

Design a site like this with WordPress.com
Get started