length = len(language) # Length of the stringuppercase_str = language.upper() # Convert to uppercaselowercase_str = language.lower() # Convert to lowercasenew_str = language.replace('P', 'J') # Replace charactersword_list = language.split('t') # Split into a list
Check Character Types
built-in methods to check if a character is of a certain type
e.g., uppercase, lowercase, alphanumeric, numerica, alpha, etc.
returns True or False
isUpper = "hello".isupper() # Checks if each character in string is uppercaseisLower = "hello".islower() # Checks if each character in string is lowercase# above 2 only check alphabet characters, not numbersisAlphaNumeric = "hello".isalnum() # Checks if alphanumericisNumeric = "hello".isnumeric() # Checks if numeric onlyisAlpha = "hello".isalpha() # Checks if alphabet only# can add `not` in front to check if it is not these things
Checking Strings
can check to determine whether a specific character or phrase exists within the string with in
strText = "The Python language can be used in network automation tasks"print("automation" in strText)
can also check if a word is not in a string with not in
print("error" not in strText)
String Slicing
String slicing in Python refers to the technique of extracting a portion of a string by specifying a range of indices.
syntax: string[start:stop:step]
start: The index from which the slicing begins (inclusive)
defaults to the beginning
stop: The index at which the slicing ends (exclusive)
defaults to the end
step (optional): The step or stride between characters
defaults to 1
text = "Hello, Python!"# Extracting a substring from index 7 to the endsubstring = text[7:]print(substring) # Output: "Python!"# Extracting the first 5 charactersfirst_five = text[:5]print(first_five) # Output: "Hello"# Extracting characters with a step of 2every_second = text[::2]print(every_second) # Output: "Hlo yhn"# Reversing a stringreversed_str = text[::-1]print(reversed_str) # Output: "!nohtyP ,olleH"
Modify Strings
strings are immutable
cannot be changed
to modify, need to create a new one with the changes
always assign to a new variable
String Length
The len() method returns the length (number of characters) of a string.
text = "Hello, Python!"length = len(text)print(length) # Output: 14
Converting Characters
upper() method converts all characters in a string to uppercase
lower()method converts all characters in a string to lowercase
join() method is used to concatenate strings from an iterable (e.g., a list)
words = ["Python", "is", "awesome"]sentence = " ".join(words)print(sentence) # Output: "Python is awesome"
Formatted Strings
The format() method in Python is a powerful and flexible way to format strings by replacing placeholders with values.
Use {} as placeholders for variables
pass variables into .format() method
Positional Arguments
Positional arguments are arguments that are passed to a function or method in a specific order.
name = "John"age = 25# Using format() method with placeholdersmessage = "My name is {} and I am {} years old.".format(name, age)print(message)# Output: "My name is John and I am 25 years old."
Named Arguments
Named arguments are arguments that are passed to a function or method by explicitly stating the name of the parameter and its value.
aka keyword arguments
allows you to specify arguments in any order
# Named arguments in format()details = "Name: {name}, Age: {age}".format(name="Alice", age=30)print(details)# Output: "Name: Alice, Age: 30"
Index-based Formatting
Index-based formatting is a feature of the str.format() method where you can specify the order in which values should be formatted.
place index numbers inside the curly braces {} that serve as placeholders in the original string
# Index-based formattingmessage = "{0} is a {1} programming language.".format("Python", "versatile")print(message)# Output: "Python is a versatile programming language."
Number Formatting
In Python, number formatting is used to control how numbers are displayed.
useful for:
display numbers in a more readable format
or control the precision of floating point numbers