Start learning free

LESSON 73 OF 673 · STRINGS - CHALLENGES

Amazing to Not Amazing in Python: String Slicing

Practice Read the full explanation

Given a word s that starts with the letter a, return "not " followed by s without its first character. That's how "amazing" becomes "not mazing"!

 

Example 1:

Input: s = "amazing"
Output: "not mazing"
Explanation: Without its first character, "amazing" is "mazing". "not " goes in front of it, with its space.

Example 2:

Input: s = "aMAZING"
Output: "not MAZING"
Explanation: Only the first character is removed. The other characters stay as they are, upper case included.

Example 3:

Input: s = "an"
Output: "not n"
Explanation: Removing the "a" leaves just "n".

 

Constraints:

  • s has at least 2 characters and starts with a lower-case a.
  • s is one word made of English letters, lowercase or uppercase (no spaces).
STEP 1 OF 1
    Ctrl + Enter to check

    PYTHON · STRINGS - CHALLENGES

    Amazing To Not Amazing in Python, explained

    Problem overview

    The method amazingToNotAmazing receives a word s that starts with a lower-case a and returns "not " followed by s without its first character. The word "amazing" becomes "not mazing".

    Only the first character is removed. For "aMAZING" the result is "not MAZING", because the other letters keep their case. For "an" the result is "not n".

    Approach

    The task has two parts: drop the first character, and put a fixed prefix in front of what is left. Python strings are immutable, so you can't delete a character in place. Instead you build a new string from a slice of the old one. A slice s[1:] starts at index 1 and, because the end index is left out, runs to the end of the string.

    A good way to check your work is to think about what each part is responsible for. The prefix "not " never depends on the input, so it is a fixed string. The slice depends only on the word, and it always drops exactly one character. If the result for "an" is "not n" and the result for "amazing" is "not mazing", both parts are doing their job.

    Step by step

    1. Slice from index 1. s[1:] is every character except the one at index 0.
    2. Add the prefix. Write "not " with its trailing space inside the quotes, then join it to the slice with +.
    3. Return the result.

    The solution in Python

    class Solution:
        def amazingToNotAmazing(self, s: str) -> str:
            return "not " + s[1:]
    

    Some details worth knowing:

    • The slice s[1:] returns a new string and leaves s as it was. "amazing"[1:] is "mazing".
    • Slices never raise an error for going past the ends. That matters little here, since the word has at least two characters, but a one-character word would simply give an empty slice.
    • The space is part of the prefix. Without it, the result would be "notmazing".

    How the slice picks characters

    A slice s[start:end] keeps the characters from index start up to, but not including, index end. Leaving out end means "to the end", and leaving out start means "from the beginning". For w = "amazing":

    • w[1:] is "mazing": everything from index 1 on.
    • w[0:] is "amazing": the whole word.
    • w[:1] is "a": the part this lesson throws away.
    • w[3:5] is "zi": a slice with both bounds.

    The first character and the slice from index 1 always add up to the original word, which is a handy check: w[:1] + w[1:] is w.

    The same idea with a named variable

    You can also keep the remaining letters in a variable before adding the prefix. For w = "aMAZING" the code below prints MAZING, then not MAZING, then aMAZING:

    w = "aMAZING"
    rest = w[1:]
    print(rest)
    print("not " + rest)
    print(w)
    

    The last line shows that w is unchanged: slicing made a new string and the original is still there. That is how you "remove" a character from a Python string: you never edit it, you build a shorter copy.

    Edge cases and common mistakes

    • Forgetting the space. "not" + s[1:] glues the words together.
    • Slicing from index 0. s[0:] is the whole string, so the a stays: "not amazing".
    • Changing case. Don't call lower() or upper(). The letters after the first stay exactly as they are.
    • Trying to assign into the string. s[0] = "" raises a TypeError, because strings don't support item assignment.

    Testing

    • "amazing" gives "not mazing".
    • "aMAZING" gives "not MAZING".
    • "an" gives "not n".
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free