check_char_in_string.pyrunning result:

The keyword in in the check_char_in_string function is used to check if a specific character is present in the given string.
It is a membership operator in Python that returns True if the character is found in the string, and False otherwise. The keyword in simplifies the process of checking for the existence of a character in a string by providing a concise way to perform this operation.
count_char_occurrence.pyrunning result:

When counting a character:
count_char_occurrence("Fudan python", "f")
→
\rightarrow
→ return 0count_char_occurrence("Fudan python", "n")
→
\rightarrow
→ return 2When counting substring:
count_char_occurrence("Fudan python", "python")
→
\rightarrow
→ return 1count_char_occurrence("aaaa", "aa")
→
\rightarrow
→ return 2count_char_occurrence("ababa", "aba")
→
\rightarrow
→ return 1The result shows that count_char_occurrence function also works for counting substring, but there still exists a problem.
3 but the function returns 2 instead2 but it returns 1Interestingly, we can see that:
count_char_occurrence function can be used to count substring,
But when we use count() method in Python to count the occurrences of overlapping substrings within a string, it does not account for overlapping occurrences of substrings, leading to a lower count than expected in certain cases.
The reason for this issue is that the count() method doesn’t consider the ending position of the previous match but continues searching from the current position. This can cause some overlapping substrings to be missed in the count.
To solve this problem, we can use regular expressions to match overlapping substrings.
import re
def count_char_occurrence_improved(input_string, char_to_check):
pattern = re.compile(f'(?={char_to_check})')
matches = re.findall(pattern, input_string)
return len(matches)
This modified function uses the “lookahead” mechanism of regular expressions, which allows it to match overlapping substrings.
running result:

get_char_index.pyrunning result:

When the character or substring is not in the input string: raise a ValueError exception
for example:
run get_char_index("Fudan python at 1:30pm", " ")
→
\rightarrow
→ ValueError: substring not found
When the character or substring is in the input string: the result is the index of the first occurrence of the specific character within a given string.
for example:
run get_char_index("Fudan python at 1:30pm", "p")
→
\rightarrow
→ return 6
run get_char_index("Fudan python at 1:30pm", " ")
→
\rightarrow
→ return 5
and it’s same when working on substrings
run get_char_index("Fudan python at 1:30pm", "at")
→
\rightarrow
→ return 13
run get_char_index("Fudan python at 1:30pm to 4:10pm", "pm")
→
\rightarrow
→ return 20
We can use a loop to iterate through the entire string to record the index each time it appears.
def get_char_indices(input_string, char_to_check):
indices = []
for i in range(len(input_string)):
if input_string[i] == char_to_check:
indices.append(i)
return indices
running result:

However, this program only supports retrieving all positions of a character in a target string and does not support retrieving substrings. Since the method for implementing the latter is similar, I will skip this part.
remove_leading_ending_spaces.pyrunning result:

run remove_leading_ending_spaces(" Fudan python ")
→
\rightarrow
→ return 'Fudan python'
run remove_leading_ending_spaces(" ")
→
\rightarrow
→ return ''
run remove_leading_ending_spaces("")
→
\rightarrow
→ return ''
extract_substring.pyrunning result:

When the start index or end index goes out of bounds, the slice operation in Python automatically adjusts the index values instead of throwing an error.
In the second function call (run extract_substring("Fudan python", 10, 100)
→
\rightarrow
→ return 'on'), when the end index exceeds the length of the string, it is adjusted to the last index of the string. Therefore, it doesn’t raise an error but instead returns the substring 'on'.
According to the third function call (run extract_substring("Fudan python", -2, 100)
→
\rightarrow
→ return 'on'), the output is as expected.
When the start index is negative, it specifies an offset relative to the end of the string. For example, -2 represents the second-to-last character, so the returned result is 'on' from the string.
list_operations.pyrunning result:

The first four functions are very similar to what have already been done for strings above. Elements in a list have similar properties as characters in a string. For example, when using the index() method, it only returns the index of the first occurrence of the specified character or element.
The only difference between them is:
It is more convenient to search for a substring in a string, while in a list, it is impossible to search for a sublist by using the code above.
add_element_to_end()running result:
