Main Menu

search

You are here

Python: Character/String Manipulation

[last updated: 2023-09-05]
Python home page
Python: Programming
-----

  • There is no 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 positions of the first character to be included,
    'end' is the index position of the last character to be included, PLUS 1.
    (This is an approximate specification, because as you can see from the examples below, it gets complicated.)
    eg.
    str1 = "abcdefghijk"
    str1[0:5]
    returns: abcde

    str1[-5:]
    returns: ghijk - ie. the last 5 characters of the string

    str1[1:-4]
    returns: bcdefg - ie. all characters except first char and last 4 chars

    str1[::2] - returns every other character

  • Remove characters from the beginning or the end of a string:
    strip() method
    eg.: [some string].strip()[-3:]
    gets the last 3 characters of the string.
    (have not tested, but it appears that...) [some string][-3:] does the same
    however if there is whitespace at the end of the string, the strip() will remove them
  • [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:
    for string1 = "say what now"
    string1[4:7] returns "wha"
    it includes the first index, but excludes the last
    string1[:2] returns "sa"
    string1[7:] returns "t now"
    string1[-3:] returns "now"
    string1[-3:][0] returns "n"

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