Python Strings
Complete Notes with Examples and Programs
Introduction to Strings
•A string is a sequence of characters enclosed in quotes: '', "", or
''' ''' .
•Strings are immutable (cannot be changed after creation).
Examples:
s1 = 'Hello’
s2 = "World"
s3 = '''This is a
multiline string'''
String Operations
a. Concatenation: Joining two or more strings using
s1 = "Hello" the + operator .
s2 = "World"
print(s1 + " " + s2) # Output: Hello World
b. Repetition:Repeating a string using the * operator .
print("Hi! " * 3) # Output: Hi! Hi! Hi!
String Operations
c. Membership: Checking for presence using in and not in.
msg = "Python is fun"
print("fun" in msg) # Output: True
print("boring" not in msg) # Output: True
d. Slicing: Extracting a part (substring) using index values:
string[start:end:step]
s = "Programming"
print(s[0:6]) # Output: Progra
print(s[:5]) # Output: Progr
print(s[3:]) # Output: gramming
print(s[::-1]) # Output: gnimmargorP (reversed string)
Traversing a String using Loops
Using for loop:
word = "Hello"
for ch in word:
print(ch)
Using while loop:
word = "Python"
i=0
while i < len(word):
print(word[i])
i += 1
Built-in String Functions/Methods
1. len(): Returns the number of characters in a string.
s = "Python"
print(len(s)) # Output: 6
2. capitalize(): Converts the first character to uppercase.
print("hello world".capitalize()) # Output: Hello world
Built-in String Functions/Methods
3. title(): Capitalizes the first character of every word.
print("hello world".title()) # Output: Hello World
4. lower(): Converts all characters to lowercase.
print("Hello".lower()) # Output: hello
Built-in String Functions/Methods
5. upper(): Converts all characters to uppercase.
print("hello".upper()) # Output: HELLO
6. count(sub): Counts occurrences of a substring.
print("banana".count("a")) # Output: 3
7. find(sub): Returns the lowest index of the substring or -1.
print("banana".find("na")) # Output: 2
Built-in String Functions/Methods
8. index(sub): Returns the lowest index of substring; error if not found.
print("banana".index("na")) # Output: 2
9. endswith(suffix): Returns True if string ends with suffix.
print("[Link]".endswith(".txt")) # Output: True
10. startswith(prefix): Returns True if string starts with prefix.
print("Hello".startswith("He")) # Output: True
Built-in String Functions/Methods
11. isalnum(): Returns True if all characters are alphanumeric.
print("Python3".isalnum()) # Output: True
12. isalpha(): Returns True if all characters are alphabetic.
print("Python".isalpha()) # Output: True
13. isdigit(): Returns True if all characters are digits.
print("12345".isdigit()) # Output: True
Built-in String Functions/Methods
14. islower(): Returns True if all characters are lowercase.
print("hello".islower()) # Output: True
15. isupper(): Returns True if all characters are uppercase.
print("HELLO".isupper()) # Output: True
16. isspace(): Returns True if all characters are whitespaces.
print(" ".isspace()) # Output: True
17. lstrip(): Removes leading spaces.
print(" hello".lstrip()) # Output: "hello"
Built-in String Functions/Methods
18. rstrip(): Removes trailing spaces.
print("hello ".rstrip()) # Output: "hello"
19. strip(): Removes both leading and trailing spaces.
print(" hello world ".strip()) # Output: "hello world"
20. replace(old, new): Replaces a substring with another.
print("I like Java".replace("Java", "Python")) # Output: I
like Python
Built-in String Functions/Methods
21. join(iterable): Joins elements of a sequence using the string as separator.
words = ["Join", "these", "words"]
print(" ".join(words)) # Output: Join these words
22. partition(sep)
Splits the string into three parts: before sep, sep, after sep.
print("hello@[Link]".partition("@"))
# Output: ('hello', '@', '[Link]')
23. split(sep): Splits the string by separator into a list.
print("a,b,c,d".split(",")) # Output: ['a', 'b', 'c', 'd']