Programming Tutorials

  • View all tutorials
  • Common Python string methods
    Below are some string functions that are commonly required in order to complete Coderbyte challenges. You can view a longer list of Python string functions at the following links: Tutorialspoint Guru99

    count

    Count how many times a character sequence occurs in a string. s = "hello world I am coding" print s.count('o') # 3

    find

    Find the index at which the string appears in the original string. s = "hello world I am coding" print s.find('world') # 6 print s.find('mars') # -1

    isalpha and isdigit

    Determine if the string is alphabetic or if it is a digit. s = "hello" n = "456" print s.isalpha() # True print s.isdigit() # False print n.isdigit() # True

    replace

    Replace a part of a string with another string. s = "hello world" print s.replace('world', 'mars') # hello mars

    swapcase

    Swap all uppercase and lowercase characters. s = "HeLLO WORLD" print s.swapcase() #hEllo world

    title

    Capitalize the first letter of each word. s = "hello world i am coding" print s.title() # Hello World I Am Coding

    join

    Join a list of strings into one string separated by the input string. nums = ['dan', 'mike', 'rob'] print ' '.join(nums) # dan mike rob print '-'.join(nums) # dan-mike-rob

    upper and lower

    Uppercase or lowercase a string. s = "hello WORLD" print s.upper() # HELLO WORLD print s.lower() # hello world
    mrdaniel wrote this tutorial on 1/3/16 | python, strings
    Comments
  • +
  • 10
  • -
  • Thanks for the useful tutorials and STEP by STEP guide.
  • +
  • 0
  • -
  • Thank you!
  • +
  • 0
  • -
  • I'd recommend beginners to Python's official website (e.g. docs.python.org/2/tutorial/index.html) rather than the suggested links. Learning a new programming language often requires details that may have been overlooked in such tutorials.
    Log in to submit a comment.