Built-in String Methods
This is a free course. Log in to run the code exercises and track your progress.
Built-in String Methods
The CBSE syllabus lists these functions and methods. None of them changes the original string; each returns a new value.
| Method | Example | Result |
|---|---|---|
len() | len("India") | 5 |
capitalize(), title() | "new delhi".title() | "New Delhi" |
lower(), upper() | "Cbse".upper() | "CBSE" |
count() | "banana".count("an") | 2 |
find(), index() | "banana".find("n") | 2 |
startswith(), endswith() | "report.csv".endswith(".csv") | True |
isalnum(), isalpha(), isdigit(), isspace(), islower(), isupper() | "2025".isdigit() | True |
strip(), lstrip(), rstrip() | " hi ".strip() | "hi" |
replace() | "a-b-c".replace("-", "+") | "a+b+c" |
split() | "a b c".split() | ['a', 'b', 'c'] |
join() | "-".join(["a", "b"]) | "a-b" |
partition() | "key=value".partition("=") | ('key', '=', 'value') |
find() returns -1 when the text is missing, while index() raises a ValueError. partition() always returns a 3-tuple: the part before the separator, the separator, and the part after it.
Read a sentence. Print the number of words on the first line, the longest word on the second (the first one if several are equally long), and the sentence with the order of its words reversed on the third.
words = input().split()
print(len(words))
longest = words[0] # find the longest word
print(longest)
print(" ".join(words)) # reverse the word orderLog in to edit and run this code.