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


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
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"
 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…:

Putting this all together…

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
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) # Output: MihirThaha
MihirThaha

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

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

Palindromes

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"))  
print(palindrome("hello"))  
print(palindrome("taco cat"))
print(palindrome("hi"))

True
False
True
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!