String | Function & Methods
Hello my name is Kishan Tongrao, we are going to discuss String’s Functions and Methods.
In Python, strings are objects, which means that they have methods and functions associated with them. Both methods and functions can be used to manipulate and work with strings, but they have some differences.
String methods are functions that are associated with a particular string object. They are called using dot notation and take the form string.method(). For example, the upper() method can be used to convert a string to uppercase:
string = "hello world"
uppercase_string = string.upper() # returns "HELLO WORLD"
String methods modify the original string object, so any changes made by the method will affect the original string. For example, the replace() method can be used to replace a substring within a string:
string = "hello world"
new_string = string.replace("hello", "hi") # returns "hi world"Pyt
String functions, on the other hand, are standalone functions that take a string as an argument. They are called using the function name and the string as the argument, and take the form function(string). For example, the len() function can be used to get the length of a string:
string = "hello world"
length = len(string) # returns 11
String functions do not modify the original string object, but instead return a new value. For example, the str() function can be used to convert a non-string object to a string:
number = 42
string = str(number) # returns "42"
Below is list of String Functions:
- len() — returns the length of the string
- str() — converts a value to a string
- ord() — returns an integer representing the Unicode character
- chr() — returns a character from the Unicode code point
- hex() — converts an integer to a hexadecimal string
- oct() — converts an integer to an octal string
# String Functions
string = "hello world"
# len()
print(len(string)) # Output: 11
# str()
number = 42
print(str(number)) # Output: '42'
# ord()
print(ord('a')) # Output: 97
# chr()
print(chr(97)) # Output: 'a'
# hex()
print(hex(255)) # Output: '0xff'
# oct()
print(oct(8)) # Output: '0o10'
Below is list of String Methods:
- lower() — returns a string in lowercase
- upper() — returns a string in uppercase
- title() — returns a string with the first letter of each word capitalized
- capitalize() — returns a string with the first letter capitalized
- strip() — removes whitespace from the beginning and end of a string
- replace() — replaces a substring with another substring
- split() — splits a string into a list of substrings
- join() — joins a list of substrings into a single string
- startswith() — checks if a string starts with a specified substring
- endswith() — checks if a string ends with a specified substring
- find() — finds the first occurrence of a substring in a string and returns its index
- count() — returns the number of occurrences of a substring in a string
- isalpha() — checks if a string contains only alphabetic characters
- isdigit() — checks if a string contains only digits
- isalnum() — checks if a string contains only alphanumeric characters
- islower() — checks if a string is in lowercase
- isupper() — checks if a string is in uppercase
- istitle() — checks if a string is in titlecase
- isnumeric() — checks if a string contains only numeric characters.
# String Methods
string = " Hello, World! "
# lower()
print(string.lower()) # Output: ' hello, world! '
# upper()
print(string.upper()) # Output: ' HELLO, WORLD! '
# title()
print(string.title()) # Output: ' Hello, World! '
# capitalize()
print(string.capitalize()) # Output: ' hello, world! '
# strip()
print(string.strip()) # Output: 'Hello, World!'
# replace()
print(string.replace("l", "X")) # Output: ' HeXXo, WorXd! '
# split()
print(string.split(",")) # Output: [' Hello', ' World! ']
# join()
substring_list = ["hello", "world"]
print(",".join(substring_list)) # Output: 'hello,world'
# startswith()
print(string.startswith(" ")) # Output: True
# endswith()
print(string.endswith("! ")) # Output: True
# find()
print(string.find("o")) # Output: 4
# count()
print(string.count("o")) # Output: 2
# isalpha()
print(string.isalpha()) # Output: False
# isdigit()
print(string.isdigit()) # Output: False
# isalnum()
print(string.isalnum()) # Output: False
# islower()
print(string.islower()) # Output: False
# isupper()
print(string.isupper()) # Output: False
# istitle()
print(string.istitle()) # Output: False
# isnumeric()
print(string.isnumeric()) # Output: False
That’s it!
Thank You