<jupyter_start><jupyter_text>String OperationsStrings can be thought of as arrays/lists of characters. As with lists, individual characters can be accessed using index notation. Remember that sequences like lists and strings are 0-indexed, meaning the first character is located at index 0, not index 1.<jupyter_code>greeting = "Hello, my name is John."
greeting[18]<jupyter_output><empty_output><jupyter_text>The comma is located at index 5 (the sixth character). Remember that spaces and newlines are characters as well in strings, even though you can't see them. Negative indexes can be used with strings as well and a portion of a string can be accessed using slice syntax:<jupyter_code>greeting = "Hello, my name is Michael."
greeting[18:-1]<jupyter_output><empty_output><jupyter_text>`split`Strings are objects in Python and they have methods that can be used to perform common string manipulations. For example the `split` method can be used to split a string into parts using the given character sequence as a delimiter. To split a string by commas (`,`):<jupyter_code>column_line = "Name,Date,Type,Value"

# Argument given to split() is the delimiter that is used to split the string
column_line.split(",")<jupyter_output><empty_output><jupyter_text>`strip`Another useful string method is the `strip` method, which can be used to "strip" white space characters from the beginning and end of strings. White space characters include spaces, tabs, newlines, and carriage returns.<jupyter_code>white_space_str = " Foo \r\n"
white_space_str.strip()<jupyter_output><empty_output><jupyter_text>ConcatenateThe `+` operator can be used to concatenate two or more strings:<jupyter_code>part1 = "Well done "
part2 = "is better than "
part3 = "well said."

quote = part1 + part2 + part3
quote<jupyter_output><empty_output><jupyter_text>`join`Alternatively, the `join` string method can be used to join a list of strings by a given delimiter. Call the `join` method on the *delimiter string* and pass the list of strings as an argument:<jupyter_code>fruit = ['Bananas', 'Apples', 'Oranges']

# Join each item by a comma and a space
joined = ', '.join(fruit)
joined<jupyter_output><empty_output><jupyter_text>Character Case MethodsThe string object also has convenience methods for changing the case of characters in the string:<jupyter_code>book = "Alice's Adventures in Wonderland"

# Convert all characters to lowercase
lower = book.lower()

# Convert all characters to uppercase
upper = book.upper()

# Capitalize the first character of every word
title = book.title()

# Capitolize only tthe first character in the string
capitalize = book.capitalize()

# Upercase letters become lowercase, and vice versa
swapcase = book.swapcase()

# Display the results
print(f'book: {book}')
print(f'lower: {lower}')
print(f'upper: {upper}')
print(f'title: {title}')
print(f'capitalize: {capitalize}')
print(f'swapcase: {swapcase}')<jupyter_output><empty_output><jupyter_text>`endswith` and `startswith`Two other helpful string methods are `endswith` and `startswith`. These can be used to determine if a string ends or starts wit the given string:<jupyter_code>web_address = "https://www.aquaveo.com"
is_secure = web_address.startswith('https')
is_commercial_site = web_address.endswith('com')

print(f"Is Secure? {is_secure}")
print(f"Is Commercial Site? {is_commercial_site}")<jupyter_output><empty_output><jupyter_text>For a list of all Python string methods, see: [String Methods | Python Documentation](https://docs.python.org/3/library/stdtypes.htmlstring-methods)  String FormattingOne of the most common operations you will perform on strings is string formatting. String formatting a mechamism that allows you to embed the values of variables in strings. Python supports several different ways of accomplishing string formatting, but in this tutorial we will only discuss the format string or f-string method, as it is the easiest. Format StringsFormat strings are similar to normal strings, but the string begins with an `f` before the first quote and variables can be embedded directly in the string at locations indicated with curly braces (`{}`) and the name of the variable. Here are a few examples to illustrate:<jupyter_code>name = 'John'
age  = 43

# Use {<var_name>} to indicate where the variable values will be substituted in the string
about_me = f'My name is {name} and my age is {age}.'
about_me<jupyter_output><empty_output><jupyter_text>One common need when formatting numbers in f-strings is to control the number of decimal places that are displayed in text dipslayed to users. This can be accomplished as follows:<jupyter_code># The decimals in a number like Euler's number (e) can be controlled in a format string as follows:
e = 2.7182

# Use :.Xf, where X is the number of digits after the decimal point to display
e_str = f"Euler's number: {e:.2f}"
e_str<jupyter_output><empty_output><jupyter_text>Another common string formatting task is to pad numbers with zeros. This is often used when naming files, for example, to ensure they sort in the correct order:<jupyter_code># Zeros can be prepended to numbers in a format string as follows
first = 1
last = 99

# Use :0X, where X indicates the number of digits
first_file = f"data_file_{first:03}.csv"
last_file = f"data_file_{last:03}.csv"

print(first_file)
print(last_file)<jupyter_output><empty_output><jupyter_text>ExerciseAdd code below each comment that performs the string operation described.<jupyter_code># Given
str_list = ['Hello', 'World']
vegatables = 'Carrot;-;Broccoli;-;Cabbage'
a_number = 25.4268912

# Join the strings in str_list variable, separated by a comma and a space, and save as str1
str1 = None

# Add an exclamation point to the end of str1 ("!") and save as str2
str2 = None

# Split vegatables into a list of vegatables with no characters other than letters and save as str3
str3 = None

# Invert the case of the characters in str2 so that uppercase letters become lowercase and vice versa, save as str4
str4 = None

# Write a format string that takes a_number and will result in the following string: "The number is: 25.427", save as str5
str5 = None


# This code checks your code
errors = False
if str1 != 'Hello, World':
    errors = True
    print(f"'{str1} is not the correct answer for str1. It should be 'Hello, World'. Please try again.")

if str2 != 'Hello, World!':
    errors = True
    print(f"'{str2} is not the correct answer for str2. It should be 'Hello, World!'. Please try again.")

if str3 != ['Carrot', 'Broccoli', 'Cabbage']:
    errors = True
    print(f"'{str3} is not the correct answer for str3. It should be ['Carrot', 'Broccoli', 'Cabbage']. Please try again.")

if str4 != 'hELLO, wORLD!':
    errors = True
    print(f"'{str4} is not the correct answer for str4. It should be 'hELLO, wORLD!'. Please try again.")

if str5 != 'The number is: 25.427':
    errors = True
    print(f"'{str5} is not the correct answer for str5. It should be 'The number is: 25.427'. Please try again.")
    
if not errors:
    print("Congratulations, you did it!")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08687693607434525, 'fraction_numerical': 0.01168684877499296, 'mean_word_length': 5.263668430335097, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '12', 'dir': 'jupyter-structured-clean-dedup', 'id': '399925', 'license': 'MIT', 'n_tokens_mistral': 2189, 'n_tokens_neox': 2089, 'n_words': 906, 'path': '06 - Strings.ipynb', 'repo_name': 'Aquaveo/python_basics', 'size': '7102'}