All courses › Programming with Python › String methods

String methods

Strings have built-in methods like .upper(), .split(sep) and .count(sub). split divides a string at a separator and returns a list of substrings. Methods always return a new value and never change the original string.

s.split(c)s.\text{split}(c)splits ss at character cc, gives a list
s.count(t)s.\text{count}(t)number of times tt occurs in ss

Symbols

ssa string
ccseparator
ttthe substring counted

Example

"a,b,c".split(",") gives ['a', 'b', 'c'].

"termo".count("m") gives 1.

Strings are immutable: s.upper() creates a new string, it does not change s.
Practise functions and data structures for free →

← Dictionary (dict) · Mutable objects and references →

Part of Programming with Python: Functions and data structures.