Table 3293. Cheatsheet of string.
backslash (\) |
backslash (\) |
This character is used as an escape character. This means it is used to introduce special character sequences that might otherwise be difficult to include in a string. When you include a backslash in a string, Python expects it to be followed by a specific character that forms an escape sequence. If the character after the backslash does not form a recognized escape sequence, Python will still process it but may not behave as you expect. |
\n |
Represents a newline character. |
\t |
Represents a tab character. |
\\ |
Represents a literal backslash (\) in the string. |
Others |
.replace() |
Introduction |
.find() |
Introduction |
string.find(substring, start, end) |
Introduction |
str.format() |
Introduction |
print(f"The sum of x and y is {x+y}.") |
Introduction, Additional capabilities |
int(True) |
Output: 1 |
raw_string = r"C:\new_folder\file.txt" |
In the raw string raw_string, the backslashes are treated as literal characters. This means that \n is not interpreted as a newline character, but rather as two separate characters, ‘’ and ‘n’. Consequently, the file path is represented exactly as it appears. |
|
Replaces a symbol/character/letter in a string |
Introduction |
Slicing and indexing in string |
Introduction |
Keyword search function/check whether or not a string is within another string (a space is included as a string character) |
Introduction |
Check if a letter/character is in a string |
Introduction |
Split a sentence/string into list of words, remove all special characters/space from a sentence |
Introduction |
Convert a text file to a string |
Introduction |
Remove/repace (part) character(s)/spaces from string |
Introduction |
Change/capitalize the case of the first letter of a string |
Introduction |
Write/save content to a text file/append a string into a text file. |
Introduction |
Generate text file with the bank of collecting all words, characters and strings from news |
Introduction |
Remove \n in string or new line in txt/text file |
Introduction |
Modify/replace the line in a text file if a line contains specific string |
Introduction |
Knuth-Morris-Pratt (KMP) algorithm (a string-searching algorithm) |
Introduction |
Extract the index of a string element in a list |
Introduction |
Put the keywords in a grouped string into the first available cells in the corresponding columns in a csv file |
Introduction |
Remove the substring after the first or last character "::" in a given string, or extract the substring between the first and last "::" |
Introduction |
Convert a csv column to a string seperated by comma |
Introduction |
Check if a string can be converted to float |
Introduction |
Remove empty strings from list of strings |
Introduction |
Extract substrings between brackets (including brackets) |
Introduction |
Extract any substrings with any pattern |
Introduction |
Remove unwanted/unnecessary parts from strings in a column of dataframe |
Introduction |
Convert strings to number (integers/float) |
Introduction |
Extract the first or last N letters from a string |
Introduction |
Check if a string is empty or space only |
Introduction |
Exception LookupError (string index) |
Introduction |
Remove empty strings from list of strings |
Introduction |
Convert all elements of specific column or in entire dataframe into strings |
Introduction |
Get the frequency of occurrence of a string in a column DataFrame |
Introduction |
Check if a CSV file contains all of the specified strings |
Introduction |
RegEx (Regular Expression) (characters to check if a string contains a specified search pattern, remove double spaces, and clean texts) |
Introduction |
Extract any substrings with any pattern |
Introduction |
Convert dataframe row/column into a comma separated string |
Introduction |
Convert/change the case of all letters/word into uppercase (capital) or lowercase in a list of strings |
Introduction |
Check if a variable is a number or string |
Introduction |
Format strings |
Introduction |
String template class for formating strings (F-strings (for calculation) (f"{}"), format() method ({}), %s, %d, Template ($)) |
Introduction |
Check if all the (and how many, length of a string) characters in the text are digits/numbers |
Introduction |
Check if a string is empty or space only |
Introduction |
Convert between numpy array, string or list of string |
Introduction |
Count how many empty strings in a list |
Introduction |
Form a list of strings from an old string with all the 6 digits by removing all special characters or spaces |
Introduction |
Remove decimal part in a string with comma |
Introduction |
Merge columns which contain specific strings |
Introduction |
Output the row into dataframe if the value of the cell in a column contains a specific substring in a csv file (with headers) |
Introduction |
Add letter/commas/numbers/characters to the end/beginning of strings in a list |
Introduction |
Remove string 0s from the end of back/end of a list until non-zero values |
Introduction |
Duplicate/repeat the same words/elements in a string/list |
Introduction |
Output data if any or same element in a string are in two lists |
Introduction |
split() |
Introduction |
splitlines(): Split a string into a list where each line is a list item. code. |
Introduction |
startswith() and endswith() |
Introduction |
strip(): Removes leading/trailing whitespace. |
Introduction |
str() |
Introduction |
string / multilinestring |
Introduction |
StringVar() |
(code) |