Python String Functions with Examples and Output
1. replace(old, new)
Example:
s = "hello world"
print([Link](" ", "-"))
Output:
hello-world
2. count(value)
Example:
s = "banana"
print([Link]("a"))
Output:
3
3. upper()
Example:
s = "python"
print([Link]())
Output:
PYTHON
4. lower()
Example:
s = "PYTHON"
print([Link]())
Output:
python
5. title()
Example:
s = "python is easy"
print([Link]())
Output:
Python Is Easy
6. capitalize()
Example:
s = "python is easy"
print([Link]())
Output:
Python is easy
7. find(value)
Example:
s = "computer"
print([Link]("p"))
Output:
3
8. startswith(value)
Example:
s = "python"
print([Link]("py"))
Output:
True
9. endswith(value)
Example:
s = "python"
print([Link]("on"))
Output:
True
10. split()
Example:
s = "python is easy"
print([Link]())
Output:
['python', 'is', 'easy']
11. strip()
Example:
s = " hello "
print([Link]())
Output:
hello
12. len()
Example:
s = "python"
print(len(s))
Output:
6
Note: Strings are immutable. Always store the returned value.