Main Menu

search

You are here

Python: Character/String Manipulation

[last updated: 2026-01-10]
Python home page
Python: Programming
-----

  • There is no separate data type for characters. A single character is simply a string data type with a length of 1.

  • Concatenation:
    str1 = "this is a"
    str2 = " test"
    str3 = str1 + str2
    print (str3)
    returns: this is a test

  • Get a substring:
    string[start: end: step]
    if step omitted, it defaults to 1
    'start' is the index position of the first character to be included,
    'end' is the index position of the character that is one past the last char to be included
    • negative index numbers:
      Negative index numbers start counting from the end of the string.
      This is confusing, because:
          if counting from the beginning of the string, the first char is index 0
          but if counting from the end of the string, the first char is index -1
      index -2 refers to the next-to-the-last char of the string, etc.
    • str01 = "abcdefg"
      str01[1:] returns 'bcdefg' (no end position defaults to the end of the string
      str01[-3:] returns 'efg', starting with index -3, which starts counting with a -1 at the end of the string
      str02[1:-3] returns 'bcd', end position specified as last char to be included+1, starting counting with -1 at last character of the string
  • str1[::2] - returns every other character

  • Get a substring using split - actually produces a list of substrings of the original string:
    a_string = "apple,banana,orange"
    fruits = a_string.split(',') # ie. splits based on a 'comma' delimiter
    print(fruits) # Output - a list of strings: ['apple', 'banana', 'orange']

  • Regular Expressions (re module) has powerful tools for extracting substrings based on complex delimiter patterns

  • Remove characters from the beginning or the end of a string:
    • Remove last char of string - Easiest IMO:
      string[:-1], ie. returns a string composed starting with the first char of the string, and ending one char before the -1 index char, which, being the last char of the string,
      the last char to be returned will be one less than that, or the next-to-the-last char of the string,
      ie, having removed the last char of the string.
    • strip() method:
      eg.: [some string].strip()[-3:]
      gets the last 3 characters of the string.
      (note that...) [some string][-3:] does the same
      however if there is whitespace at the end of the string, the strip() will remove them
      Complication:
        Basically, this will remove any chars inside the () from the end of the string, with an empty() interpreted as command to remove whitespace.
        However if a char to be removed is specified, eg: str01.strip("g")
        then All "g"s at the end of the string will be removed, up to the first non-g character

      --------

    • [string1].find([string2] will return -1 if string 2 is not found in string1
      If it is there, the position of the first character (zero-indexed) will be returned.
      eg. string1 = "this string", string2 = "is", string1.find(string2) will return 2
    • Slicing: [this is ambiguous, because while it's called "slicing", it does not use the .slice command...]
      for string1 = "say what now"
      it includes the first index, but excludes the last
      string1[:2] returns "sa" (the 0 index start position is assumed)
      string1[7:] returns "t now" (the end char is assumed to be the last char of the string)
      string1[-3:] returns "now"

    • Get number of words in a string:
        a_string = "one two three"
        word_list = a_string. split() Split `a_string` by whitespace.
        number_of_words = len(word_list)
        print(number_of_words)

    • Get number of characters in a string:
        a_string = "someText"
        number_of_chars = len(a_string)
        print(number_of_chars)
        the "someText" string will return a length of 8

    • Get Nth word in String:
        # initializing string
        test_str = "GFG is for Geeks"

        # printing original string
        print("The original string is : " + test_str)

        # initializing N
        N = 3

        # Get Nth word in String
        # using loop
        count = 0
        res = ""
        for ele in test_str:
        if ele == ' ':
        count = count + 1
        if count == N:
        break
        res = ""
        else :
        res = res + ele

        # printing result
        print("The Nth word in String : " + res)
        Output :

        The original string is : GFG is for Geeks
        The Nth word in String : for

    • Convert String to list of words:
      • this worked...
        wordList = inStr.split()
        wordList = inStr.split(" ") - this seemed to do the same ...
      • from stackExchange, I think, but didn't work...
        import re
        mystr = 'This is a string, with words!'
        wordList = re.sub("[^\w]", " ", mystr).split()

    • Remove a specific word from a string:
      s = s.replace('papa', '')
    • Remove nth word from string:
      • nreplace=1
        my_string="hello my friend"
        words=my_string.split(" ")
        words[nreplace]="your"
        " ".join(words)
      • OR -
        You can replace the 4th list item using
        my_list[3] = "new"
        And then put it back together with
        my_new_string = " ".join(my_list)
      • del inStr[2]
        will delete the 2nd index element
        del inStr[2:4]
        will delete 2nd & 3rd index element

    • Join elements of a list into a single string:
      wordList = ['this', 'is', 'a', 'sentence']
      newStr = ' '.join(wordList)

    .

    .

    .

    eof