String Operations in Python

Strings are an ordered sequence of characters, which can include letters, numbers, or even spaces. A substring is part of an existing string. Various procedures are available in Python for working with strings.

Len Function

The len function returns the number of characters in a string.

For example:

len ("APCSP") # Output is 5
len ("APCSP")
5

This function will return 5, which is the number of characters in the string “APCSP”

Concat Feature

The concat function merges both strings together. For example, if I wanted to say Hi Bob, instead of writing “Hi Bob” as a string, I could concat the string to say Hi + Bob. For example:

concat = "Hi" + "Bob"

print(concat) #Output is HiBob

concat = "Hi" + "Bob"

print(concat)


HiBob

Note that you don’t actually have to use concat as the name of the variable, the concat serves as a function you can do even when the variable is not named concat. For example:

story = "The quick brown fox " + "jumped over the lazy dog"

print(story) # Output is The quick brown fox jumped over the lazy dog


```python
story = "The quick brown fox " + "jumped over the lazy dog"

print(story)
The quick brown fox jumped over the lazy dog

Now, let’s talk about substrings. Substrings are portions of strings. In python, you can extract substrings by specifying the start and end index positions of the characters that you want. Let’s try it out with the story variable we just created!

storysubstring = story[3:9]

print(storysubstring)

# Output is "quick"
storysubstring = story[3:9]

print(storysubstring)

# Output is "quick"
 quick

As you can see, we specified which characters we wanted using brackets following the variable of choice. The output of the command was “quick”, as quick is the first 3-9 characters of the string story, or asa we learned, “quick” is the substring.

Now let’s recap what we’ve learned with…:

Popcorn Hack #4

The goal of this hack is to find the third-sixth characters of my full name. Let’s start!

Here we’re going to start off by finding the number of characters in our last name. I’ll be using my last name as an example. I’ll first make it a variable, then find the length of it.

last_name = "Thaha"
length = len(last_name)

print(length) # Output: 5
last_name = "Thaha"
length = len(last_name)

print(length) # Output: 5
5

I’ll now use the concat feature to merge my first and last name.

first_name = "Mihir"

last_name = "Thaha"

full_name = first_name + last_name

print(full_name)
MihirThaha
first_name = "Mihir"

last_name = "Thaha"

full_name = first_name + last_name

print(full_name) # Output is MihirThaha

Finally, let’s use the substring feature to find the 3rd to 6th characters in my last name

namesubstring = full_name[2:6]   # Note that we use 2 instead of three, because the counting starts at 0 instead of one. If we want to start from the third character, we'd need to start from 2.
print(namesubstring)
# Output is hirT



```python
namesubstring = full_name[2:6]
print(namesubstring)
hirT

Palindromes Hack

A palindrome is a string that reads the same from left to right. For example, 5005 and racecar are both palindromes because they read the same from left to right.

def palindrome(input_str):
    # Remove spaces and convert the string to lowercase
    clean_str = input_str.replace(" ", "").lower()

    # Check if the cleaned string is equal to its reverse
    return clean_str == clean_str[::-1]

# Example usage
print(palindrome("A man a plan a canal Panama"))  # Should return True
print(palindrome("hello"))  # Should return False

True
False
def palindrome(input_str):
    # Remove spaces and convert the string to lowercase
    clean_str = input_str.replace(" ", "").lower()

    # Check if the cleaned string is equal to its reverse
    return clean_str == clean_str[::-1]

# Example usage
print(palindrome("A man a plan a canal Panama"))  # Should return True
print(palindrome("hello"))  # Should return False

def palindrome(input_str):
    # Remove spaces and convert the string to lowercase
    clean_str = input_str.replace(" ", "").lower()
    # Check if the cleaned string is equal to its reverse
    return clean_str == clean_str[::-1]


print(palindrome("taco cat")) # True
print(palindrome("hi")) # False

True
False


    return clean_str == clean_str[::-1]




print(palindrome("taco cat")) # True
print(palindrome("hi")) # False

This function checks if the string is the same forward as it is reverse

If it is a palendrome, the function will output True, and if it’s not then the function will output false!

Reverse Order Hack

Take a sentence as input, then reverse the order of words, but not the characters within the words.

Instructions:

Split the sentence into words, reverse the word list, and then join them back into a string. Try this with different sentences!

def reverse_words(input_str):
    words = input_str.split()
    reversed_words = " ".join(words[::-1])
    return reversed_words

sentence = "I love coding!"
print(reverse_words(sentence))
def reverse_words(input_str):
    words = input_str.split()
    reversed_words = " ".join(words[::-1])
    return reversed_words

sentence = "I love coding!"
print(reverse_words(sentence))

These hacks allow you to experiment with string manipulations such as counting character occurrences and reversing word orders.

Remove Wovels

Write a function that takes a sentence as input and removes all the vowels (a, e, i, o, u) from it, both uppercase and lowercase. The program should return the modified sentence.

Instructions:

Use a loop or list comprehension to filter out vowels. Test it with sentences that contain both uppercase and lowercase vowels.

def remove_vowels(input_str):
    vowels = "aeiouAEIOU"
    result = ''.join([char for char in input_str if char not in vowels])
    return result

sentence = "I enjoy learning Python!"
print(remove_vowels(sentence))

def remove_vowels(input_str):
    vowels = "aeiouAEIOU"
    result = ''.join([char for char in input_str if char not in vowels])
    return result

sentence = "I enjoy learning Python!"
print(remove_vowels(sentence))

This hack encourages practice with loops or list comprehensions and string filtering!