In this video, I will show you how to use strings as arrays in Python. We will start with basic string indexing, demonstrating how to get a character at a specific position in a string, slice a portion of a string, and use negative indexing to access parts of the string. Finally, we’ll show you how to get the length of a string. By following these examples, you will master various techniques for manipulating strings in Python. Subscribe to my channel for more Python programming tutorials.
1. Video.
2. Python Source Code.
# Get the Character at a Specific Position def get_char_at_position(): # Define a string a = "Python Programming!" # Get the character at position 1 (remember, the first character is at position 0) char = a[1] print(f"The character at position 1 is: {char}") # Slice a Portion of the String def slice_string(): # Define a string b = "Python Programming!" # Get characters from position 3 to position 7 (not included) sliced = b[3:7] print(f"The characters from position 3 to 7 are: {sliced}") # Slice Using Negative Indexing def negative_index_slice(): # Define a string b = "Python Programming!" # Get characters from position -6 to position -2 (not included) sliced_negative = b[-6:-2] print(f"The characters from position -6 to -2 are: {sliced_negative}") # Get the Length of a String def get_string_length(): # Define a string a = "Python Programming!" # Get the length of the string length = len(a) print(f"The length of the string is: {length}") # Call in main function if __name__ == "__main__": get_char_at_position() slice_string() negative_index_slice() get_string_length()