Slicing Strings:
You can slice strings using the syntax [start:stop:step]
, where:
start
: The starting index of the slice (inclusive).stop
: The ending index of the slice (exclusive).step
: The step or increment between characters (optional).
If you try to access an index that’s larger than the length of your string, you’ll get an IndexError. This is because you’re trying to access something that doesn’t exist!
You can also access indexes from the end of the string going towards the start of the string by using negative values. The index [-1] would access the last character of the string, and the index [-2] would access the second-to-last character.
Example:
my_string = "Python Programming"
# Slicing from index 7 to the end
substring1 = my_string[7:]
print(substring1) # Output: Programming
# Slicing from index 0 to 6
substring2 = my_string[:6]
print(substring2) # Output: Python
# Slicing from index 7 to 13 with step 2
substring3 = my_string[7:13:2]
print(substring3) # Output: Porm
string1 = "Greetings, Earthlings"
print(string1[0]) # Prints “G”
print(string1[4:8]) # Prints “ting”
print(string1[11:]) # Prints “Earthlings”
print(string1[:5]) # Prints “Greet”
If your index is beyond the end of the string, Python returns an empty string.
An optional way to slice an index is by the stride argument, indicated by using a double colon.
This allows you to skip over the corresponding number of characters in your index, or if you’re using a negative stride, the string prints backwards.
print(string1[0::2]) # Prints “Getns atlns”
print(string1[::-1]) # Prints “sgnilhtraE ,sgniteerG”
Using the str.format()
method String Formatting
The str.format()
method is a powerful tool in Python for string formatting. It allows you to create dynamic strings by inserting values into placeholders within a string template. Here’s a basic overview of how it works:
Basic Usage:
You start with a string containing placeholder curly braces {}
where you want to insert values, and then you call the format()
method on that string with the values you want to insert.
Example:
name = "John"
age = 30
print("My name is {} and I am {} years old.".format(name, age))
My name is John and I am 30 years old.
Positional Arguments:
You can pass values to the format()
method in the order that corresponds to the order of the placeholders in the string.
Example:
print("My name is {} and I am {} years old.".format("Alice", 25))
My name is Alice and I am 25 years old.
Keyword Arguments:
Alternatively, you can pass values using keyword arguments, where the keys match the names of the placeholders.
Example:
print("My name is {name} and I am {age} years old.".format(name="Bob", age=28))
Output:
My name is Bob and I am 28 years old.
Formatting:
You can also specify formatting options within the placeholders to control the appearance of the inserted values, such as precision for floating-point numbers or padding for strings.
Example:
pi = 3.14159
print("The value of pi is {:.2f}".format(pi))
Output:
The value of pi is 3.14
Padding and Alignment:-
You can align strings and pad them with spaces or other characters.
left_aligned = "{:<10}".format("left")
right_aligned = "{:>10}".format("right")
center_aligned = "{:^10}".format("center")
print(left_aligned)
print(right_aligned)
print(center_aligned)
Accessing Arguments by Position:
You can access arguments out of order and even multiple times by specifying their positions within the curly braces.
Example:
print("{1} {0} {1}".format("be", "or", "not"))
Output:
or be or
Guess the ERROR:-
Head to Next
Discover more from HintsToday
Subscribe to get the latest posts sent to your email.