+91 81900 98289

String Methods

This String Methods lesson is for a beginner. Read the short notes, run each example, then change one name or number and run it again.

split breaks text into a list. join builds text from a list. startswith, endswith, count, and find answer questions about the contents.

These methods never change the original string. They return a new value.

Example 1: Start here

split with no argument breaks on whitespace and drops empty pieces.

line = "Python,SQL,Git" # text print(line.split(",")) # split breaks the text into pieces print(line.startswith("Python")) # True or False print(line.count("S")) # how many times it appears

Example 2: Find a word

find returns the starting position. The first letter is position 0.

print("learn python".find("python")) # the starting position, counted from 0

Example 3: Count a letter

count tells you how many times the letter appears.

print("banana".count("a")) # how many times it appears

Example 4: Check the ending

endswith is a True or False test.

print("notes.txt".endswith(".txt")) # True or False
split with no argument breaks on whitespace and drops empty pieces.
Call Now 81900 98289