Introduction
Welcome to the comprehensive guide on Python’s built-in string functions. Strings are a fundamental data type in Python, and the language equips programmers with a rich set of functions to efficiently handle and manipulate them.
In this lesson, we will explore 40 essential string functions, covering everything from basic formatting to advanced searching and encoding.
Whether you’re a beginner looking to strengthen your Python skills or an experienced developer aiming to deepen your understanding, this guide will provide valuable insights and practical examples to enhance your proficiency in string manipulation.
Python provides a rich set of built-in functions to manipulate strings. These functions allow you to perform common operations such as formatting, searching, and manipulation of strings.
There are 40 built-in string functions in Python. Some of the most commonly used string functions include lower(), upper(), strip(), split(), replace(), and join().
The lower() function returns a copy of the string in lowercase letters.
The upper() function returns a copy of the string in uppercase letters.
The strip() function removes whitespace characters from the beginning and end of the string. The split() function breaks up a string into a list of substrings based on a separator string.
The replace() function replaces occurrences of a substring with another substring.
The join() function concatenates a list of strings with a separator string.
Other string functions provide additional functionality, such as searching for substrings, padding strings with characters, and checking if a string contains only certain types of characters.
By mastering these built-in string functions, you can efficiently and effectively manipulate strings in your Python programs.
As of Python 3.10, there are a total of 40 built-in string functions in Python.
Here is the list of all 40 functions:
The functions in Python with an example
used to returns the length of a string
string = "Hello, world!" print(len(string)) # Output: 13
used to converts a string to lowercase
string = "Hello, world!" print(string.lower()) # Output: hello, world!
uses to converts a string to uppercase
string = "Hello, world!" print(string.upper()) # Output: HELLO, WORLD!
used to removes whitespace from the beginning and end of a string
string = " Hello, world! " print(string.strip()) # Output: "Hello, world!"
uses to replaces a substring with another substring
string = "Hello, world!" print(string.replace("Hello", "Hi")) # Output: Hi, world!
used to splits a string into a list of substrings based on a delimiter
string = "Hello, world!" print(string.split(",")) # Output: ["Hello", " world!"]
startswith(): checks if a string starts with a specified substring
string = "Hello, world!" print(string.startswith("Hello")) # Output: True
endswith(): checks if a string ends with a specified substring
string = "Hello, world!" print(string.endswith("!")) # Output: True
checks if a string contains only alphanumeric characters
string = "Hello123" print(string.isalnum()) # Output: True
checks if a string contains only alphabetic characters
string = "Hello" print(string.isalpha()) # Output: True
checks if a string contains only digits
string = "123" print(string.isdigit()) # Output: True
checks if a string is all lowercase
string = "hello" print(string.islower()) # Output: True
checks if a string is all uppercase
string = "HELLO" print(string.isupper()) # Output: True
joins a list of strings with a delimiter
words = ["Hello", "world", "!"] delimiter = "-" print(delimiter.join(words)) # Output: "Hello-world-!"
converts the first character of each word to uppercase
string = "hello, world!" print(string.title()) # Output: Hello, World!
converts the first character of a string to uppercase and the rest to lowercase.
string = "hello, world!" print(string.capitalize()) # Output: Hello, world!
returns a lowercase version of the string, suitable for case-insensitive comparisons.
string = "HELLO, WORLD!" print(string.casefold()) # Output: hello, world!
returns a string centered within a specified width with optional fill characters.
string = "Hello" print(string.center(10, "-")) # Output: --Hello---
returns the number of occurrences of a substring in a string.
string = "Hello, world!" print(string.count("l")) # Output: 3
returns an encoded version of the string.
string = "Hello, world!" print(string.encode()) # Output: b'Hello, world!'
replaces tab characters in a string with one or more spaces.
string = "Hello\tworld!" print(string.expandtabs()) # Output: Hello world!
searches for a substring in a string and returns its index. Returns -1 if the substring is not found.
string = "Hello, world!" print(string.find("world")) # Output: 7
searches for a substring in a string and returns its index. Raises a ValueError if the substring is not found.
string = "Hello, world!" print(string.index("world")) # Output: 7
checks if a string contains only decimal characters.
string = "123" print(string.isdecimal()) # Output: True
checks if a string contains only numeric characters.
string = "½" print(string.isnumeric()) # Output: True
checks if a string contains only whitespace characters.
string = " " print(string.isspace()) # Output: True
returns a left-justified version of a string with optional fill characters.
string = "Hello" print(string.ljust(10, "-")) # Output: Hello-----
returns a right-justified version of a string with optional fill characters.
string = "Hello" print(string.rjust(10, "-")) # Output: -----Hello
removes whitespace from the beginning of a string.
string = " Hello" print(string.lstrip()) # Output: "Hello"
removes whitespace from the end of a string.
string = "Hello " print(string.rstrip()) # Output: "Hello"
concatenates a list of strings with a separator string.
separator = ", " words = ["apple", "banana", "cherry"] print(separator.join(words)) # Output: "apple, banana, cherry"
separates a string into three parts based on a separator string. Returns a tuple with the three parts.
string = "Hello, world!" print(string.partition(",")) # Output: ('Hello', ',', ' world!')
replaces all occurrences of a substring in a string with another substring.
string = "Hello, world!" print(string.replace("world", "Python")) # Output: "Hello, Python!"
searches for a substring in a string and returns its index, starting from the end of the string. Returns -1 if the substring is not found.
string = "Hello, world!" print(string.rfind("o")) # Output: 8
searches for a substring in a string and returns its index, starting from the end of the string. Raises a ValueError if the substring is not found.
string = "Hello, world!" print(string.rindex("o")) # Output: 8
splits a string into a list of substrings based on a separator string.
string = "apple,banana,cherry" print(string.split(",")) # Output: ['apple', 'banana', 'cherry']
splits a string into a list of substrings based on line breaks.
string = "Hello\nworld" print(string.splitlines()) # Output: ['Hello', 'world']
checks if a string starts with a specified substring.
string = "Hello, world!" print(string.startswith("Hello")) # Output: True
checks if a string ends with a specified substring.
string = "Hello, world!" print(string.endswith("world!")) # Output: True
pads a string with zeros on the left until it reaches a specified length.
string = "42" print(string.zfill(5)) # Output: "00042"
This lesson delves into the extensive array of built-in string functions in Python, offering a detailed exploration of 40 essential methods.
From basic transformations like lower() and upper() to advanced techniques such as pattern
searching and encoding, this guide empowers learners to harness the full capability of Python’s string manipulation capabilities. Each function is explained with clarity, accompanied by real-world examples to solidify understanding. Elevate your Python programming proficiency by mastering these fundamental string functions.
a) Converts a string to uppercase
b) Returns a copy of the string in lowercase letters
c) Removes whitespace from the beginning and end of the string
d) Splits a string into a list of substrings based on a separator string
a) startswith()
b) endswith()
c) isalpha()
d) islower()
a) replace()
b) split()
c) strip()
d) isdigit()
a) Converts the first character of each word to uppercase
b) Joins a list of strings with a separator string
c) Returns a copy of the string in uppercase letters
d) Checks if a string starts with a specified substring
a) rfind()
b) index()
c) find()
d) endswith()
a) isdigit()
b) isnumeric()
c) isalpha()
d) islower()
a) Splits a string into a list of substrings based on a separator string
b) Splits a string into a list of substrings based on line breaks
c) Checks if a string starts with a specified substring
d) Removes whitespace from the beginning and end of the string
a) rjust()
b) ljust()
c) zfill()
d) strip()
a) Converts a string to lowercase
b) Converts the first character of a string to uppercase and the rest to lowercase
c) Returns a lowercase version of the string, suitable for case-insensitive
comparisons
d) Checks if a string contains only decimal characters
a) lstrip()
b) rstrip()
c) strip()
d) replace()
Answers:
b) Returns a copy of the string in lowercase letters
a) startswith()
a) replace()
b) Joins a list of strings with a separator string
a) rfind()
b) isnumeric()
b) Splits a string into a list of substrings based on line breaks
c) zfill()
b) Converts the first character of a string to uppercase and the rest to lowercase
b) rstrip()
a) Converts a string to uppercase
b) Converts the first character of each word to uppercase
c) Returns a lowercase version of the string, suitable for case-insensitive
comparisons
d) Checks if a string contains only alphanumeric characters
a) isspace()
b) isalpha()
c) isnumeric()
d) isdigit()
a) Returns the length of a string
b) Returns the number of occurrences of a substring in a string
c) Replaces all occurrences of a substring in a string with another substring
d) Splits a string into a list of substrings based on a delimiter
a) encode()
b) decode()
c) expandtabs()
d) capitalize()
a) Removes whitespace from the beginning and end of the string
b) Replaces tab characters in a string with one or more spaces
c) Checks if a string ends with a specified substring
d) Pads a string with zeros on the left until it reaches a specified length
a) ljust()
b) rjust()
c) center()
d) strip()
a) Separates a string into three parts based on a separator string
b) Replaces all occurrences of a substring in a string with another substring
c) Checks if a string ends with a specified substring
d) Joins a list of strings with a separator string
a) isalpha()
b) isalnum()
c) isdigit()
d) islower()
a) Separates a string into three parts based on a separator string
b) Removes whitespace from the end of a string
c) Joins a list of strings with a separator string
d) Returns a right-justified version of a string with optional fill characters
a) Removes whitespace from the beginning of a string
b) Removes whitespace from the end of a string
c) Joins a list of strings with a separator string
d) Returns a copy of the string in uppercase letters
Answers:
c) Returns a lowercase version of the string, suitable for case-insensitive
comparisons
a) isspace()
b) Returns the number of occurrences of a substring in a string
a) encode()
b) Replaces tab characters in a string with one or more spaces
a) ljust()
a) Separates a string into three parts based on a separator string
a) isalpha()
a) Separates a string into three parts based on a separator string
b) Removes whitespace from the end of a string
Question 21:
What is the purpose of the isalnum() function?
a) Checks if a string contains only alphabetic characters
b) Checks if a string contains only alphanumeric characters
c) Checks if a string contains only numeric characters
d) Checks if a string contains only whitespace characters
a) rindex()
b) index()
c) find()
d) rfind()
a) Checks if a string starts with a specified substring
b) Replaces all occurrences of a substring in a string with another substring
c) Joins a list of strings with a separator string
d) Formats a string by replacing placeholders with values
a) rjust()
b) ljust()
c) center()
d) strip()
a) Checks if a string starts with a specified substring
b) Replaces all occurrences of a substring in a string with another substring
c) Joins a list of strings with a separator string
d) Formats a string by replacing placeholders with values from a dictionary
a) ljust()
b) zfill()
c) rjust()
d) center()
a) Checks if a string contains only alphanumeric characters
b) Checks if a string contains only numeric characters
c) Checks if a string contains only whitespace characters
d) Checks if a string contains only uppercase characters
a) islower()
b) isupper()
c) isdigit()
d) isalpha()
a) Returns an encoded version of the string
b) Replaces tab characters in a string with one or more spaces
c) Checks if a string starts with a specified substring
d) Splits a string into a list of substrings based on a delimiter
a) isdecimal()
b) isdigit()
c) isnumeric()
d) isalpha()
Answers:
b) Checks if a string contains only alphanumeric characters
b) index()
d) Formats a string by replacing placeholders with values
a) rjust()
d) Formats a string by replacing placeholders with values from a dictionary
b) zfill()
b) Checks if a string contains only numeric characters
a) islower()
a) Returns an encoded version of the string
a) isdecimal()