<jupyter_start><jupyter_text>[String methods](https://docs.python.org/3/library/stdtypes.htmlstring-methods)You already know some methods to modify strings in Python. In this unit, you will get to know some more possible stringoperations. Shown in the table below is a selection of common methods and a short description. They will be explainedmore detailed below.| Method         | Description                                                                                  || -------------- | -------------------------------------------------------------------------------------------- || `count()`      | Count occurrences of substrings                                                              || `replace()`    | Replace a substring                                                                          || `strip()`      | Deletes spaces, tabs, linebreaks and similar at the beginning and the end of a string        || `split()`      | Return a list of the string elements, separated by given separator                           || `join()`       | Join input strings, separated by string invoking the method                                  || `upper()`      | Make all characters *UPPERCASE*                                                              || `lower()`      | Make all characters *lowercase*                                                              || `title()`      | Make the first letter of every *word* in the **string** *UPPERCASE* and the rest *lowercase* || `capitalize()` | Change the first *letter* of a **string** to *UPPERCASE* while making the rest *lowercase*   |The following explanations are based on the notation of the[official Python documentation](https://docs.python.org/3/library/stdtypes.htmlstring-methods) where:- *str* is just a placeholder for the `String` object, from which the method is invoked- arguments in square brackets *[ ]* are optional- default values for arguments (if any) are notated with an *equal sign* ( = )  Counting with *`str.`***`count`**`(sub`*`[,start][,end]`*`)`Using this method you get the number of times *`sub`* is contained in *`str`*. You can use the 2 optional arguments torestrict counting to specific parts of the string using the same notation for slicing as in week 2, unit 7.  Try out different arguments for this method. Can you spot anything special about line 5?<jupyter_code>test_string = "This this is a string containing some letters: xyxyx and a few numbers: 123 456 778899"
print(test_string.count("T"))
print(test_string.count("xyx"))<jupyter_output><empty_output><jupyter_text>Replacing parts of a string using *`str.`***`replace`**`(old, new`*`[,count]`*`)`If you want to replace parts of a string, you can use the method mentioned above. It will replace *old* by *new* and youcan specify to do it only *count* times. However, it is important to notice that the method will return a copy of themodified string and not modify the original one.<jupyter_code>string_modified = test_string.replace("This this", "Look here, this")
print(f"The string ({test_string}) looks like this after modifying: {string_modified}")

print(test_string.replace("xy", "xyz", 1))<jupyter_output><empty_output><jupyter_text>Splitting strings or lines of files with *`str.`***`split`**`(sep=None`*`[,maxsplit=-1]`*`)`When reading data from a file, you may come across `.csv` files, for example from measured data or exporting a list ofstudents from a web tool. The data is written in a text file with a special ending (`.csv`) where rows are separated byline breaks and the columns are separated by a delimiter - usually the comma, hence the name*[(csv - comma-separated-values)](https://en.wikipedia.org/wiki/Comma-separated_values)*.Usually commas (`,`) are used to isolate the data sets from each other, but also a semicolon (`;`) or other signs likespaces or even tab stops (`\t`) can be used. So be sure to take a look at your input data to determine the delimiterbeforehand.  With the method `.split()` you can extract the values from the lines in the file and assign them to separate list itemslike shown below.<jupyter_code>list_of_students = []
with open("students.csv", "r") as f:
    for line in f:
        line = line.strip()
        student = line.split(";")
        list_of_students.append(student)

print(
    f"Specified student in list: {list_of_students[int(input('Which list element do you want to show?'))]}"
)<jupyter_output><empty_output><jupyter_text>Joining strings or lines again using *`str.`***`join`**`(iterable)`If you would like to add your previously split student list together again, you can use `join()` to concatenate any*iterable* (e.g. list, tuple) of strings. You will get an output string which is separated by the string the method isinvoked from.<jupyter_code>separator_string = ","
separator2 = ", "

string_list = ["test1-string", "another one", "end"]
print(separator_string.join(string_list))

print(separator2.join(list_of_students[0]))<jupyter_output><empty_output><jupyter_text>Changing the letter case *`str.`***`upper`**`()` and *`str.`***`lower`**`()`Whenever you want to change the case of a string, you can use `.upper()` or `.lower()` to make your string all*UPPERCASE* or completely lowercase. It will return a modified copy of the string.  This may be interesting when you want to design your program independent from case-sensitive search terms for example  *`str.`***`title`**`()`With `.title()` you can capitalize every first letter of a word in a string whereas `.capitalize()` only makes the firstletter in a string *UPPERCASE*.<jupyter_code>case_string = "tHIS Is a TeSTString in different caSES"

print(case_string.upper())
print(case_string.lower())
print(case_string.title())
print(case_string.capitalize())<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12279488066413007, 'fraction_numerical': 0.0041508128675198895, 'mean_word_length': 3.2273391812865495, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '227952', 'license': 'CC0-1.0', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1486, 'n_words': 637, 'path': 'week_4/week_4_unit_6_stringmethods_notebook.ipynb', 'repo_name': 'ceedee666/opensap_python_intro', 'size': '5782'}