Start learning free

LESSON 72 OF 287 · STRINGS - CHALLENGES

Concat First and Last Char in Python: Indexing

Practice Read the full explanation

Given a string s, return a new string made of two characters: the first character of s, followed by its last character.

 

Example 1:

Input: s = "hello"
Output: "ho"
Explanation: The first character is "h" and the last one is "o".

Example 2:

Input: s = "Hello World!"
Output: "H!"
Explanation: Every character counts, so the last one is the "!", not the "d". The "H" stays upper case.

Example 3:

Input: s = "ab"
Output: "ab"
Explanation: With only two characters, the first is "a" and the last is "b".

 

Constraints:

  • s has at least 2 characters.
  • s contains English letters, digits, spaces and punctuation, and doesn't start or end with a space.
STEP 1 OF 1
    Ctrl + Enter to check

    PYTHON · STRINGS - CHALLENGES

    Concat First And Last Char in Python, explained

    Problem overview

    The method concatFirstAndLastChar receives a string s with at least two characters and returns a new two-character string: the first character of s followed by its last character. Every character counts, spaces and punctuation included, and letters keep their case.

    For s = "hello" the first character is h and the last is o, so the result is "ho". For "Hello World!" the last character is the exclamation mark, so the result is "H!". For "ab" the two characters are the whole string, and the result is "ab".

    Approach

    You need two single characters and the + operator to join them. Indexing gives you each character. The first one is at index 0. The last one depends on the length of the string, but Python lets you count from the end with a negative index: -1 is the last character, -2 the one before it, and so on. That avoids computing len(s) - 1 yourself.

    Step by step

    1. Take the first character with s[0].
    2. Take the last character with s[-1].
    3. Join them with +. Adding two strings builds a new string, so the original s is untouched.
    4. Return the result.

    The solution in Python

    class Solution:
        def concatFirstAndLastChar(self, s: str) -> str:
            return s[0] + s[-1]
    

    Some details worth knowing:

    • Indexing a string gives another string of length 1. Python has no separate character type, so s[0] + s[-1] is plain string concatenation.
    • With a negative index, s[-1] means s[len(s) - 1]. It works for any length, so the same code handles "ab" and a long sentence.
    • Strings are immutable. Nothing about s changes; the function builds and returns a new string.

    How positive and negative indexes line up

    Every character of a string has two valid indexes: one counted from the front, starting at 0, and one counted from the back, starting at -1. For "hello":

    characterhello
    from the front01234
    from the back-5-4-3-2-1

    Both rows pick the same characters. In "Hello World!", which has 12 characters, s[0] is H and both s[-1] and s[11] are !. The negative form is shorter and doesn't depend on the length, so it is the usual choice for "the last one".

    The same idea with named variables

    When a one-line expression feels dense, split it into steps. This version behaves exactly like the solution above, and for "Hello World!" it prints H and ! on the first line, then H!:

    s = "Hello World!"
    first = s[0]
    last = s[-1]
    print(first, last)
    print(first + last)
    

    Naming the pieces makes each step readable and easy to print while you debug. The one-line form is the same thing without the temporary names. Use whichever you can read at a glance, and test both against the examples.

    Edge cases and common mistakes

    • A two-character string. "ab" gives "ab": index 0 and index -1 are different positions, so each character is used once. The constraints guarantee at least two characters.
    • Too short a string. The lesson never passes one, but an empty string would fail: ""[0] raises IndexError: string index out of range.
    • Using s[len(s)] for the last character. Valid indexes stop at len(s) - 1, so this raises the same IndexError. Use -1.
    • Trailing punctuation. The last character of "Hello World!" is !, not d. Don't skip punctuation or spaces.

    Testing

    • "hello" gives "ho".
    • "Hello World!" gives "H!".
    • "ab" gives "ab".
    • "xyz" gives "xz".
    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free