[PYTHON] Replace and delete strings

Replace and delete strings

When dealing with strings in programming, we often perform the process of replacing part of the string. You can use the replase () method of a string object to replace a part of a string with another string. [Example] orig_str = "Hello" orig str.replace (“ko”, “a”) ↓ Hello

When you call the replase () method, the result of replacing the string will be returned as a new string. The string object used to call the method is unchanged. * Character strings are data types that cannot be changed

The replase () method can also be used to remove a character by passing an empty string as the second argument. The specified string will be replaced with an empty string and will be deleted as a result. For example, when treating a character string equivalent to an integer with a comma every 3 digits as a number, if you simply convert the character string to a number You can use the built-in function int (). However, if you enter a symbol like a comma, the int () function will cause an error. You can use the replase () method to remove unnecessary strings in advance, and then use the int () function to convert it to a number. [Example] str_num = “1,000,000” num = int(str_num.replase(“,” , “”)) num ↓ 1000000

split () method You can use the split () method to split a string around a specific character. Use the split () method to split a tab, a blank string like a space, or a long string separated by commas into smaller strings. Spreadsheet software such as Excel allows you to export the contents of a table in a tab-separated format. Also, data that can be downloaded from the Internet may be distributed in a similar format. Use the split () method when importing such data into Python and processing it. The split () method passes the delimiter string used for splitting as an argument and returns the result as a list of strings. In addition, CSV format files written by spreadsheet software such as Excel are complicated in format and difficult to handle with the split () method.

join () method You can use the join () method to do the opposite of the split () method. The join () method takes a list that has a character string as an element as an argument, and can get the character string that is concatenated from the list. When concatenating, call the join () method using the concatenated string between them. For example, suppose you want to convert a string of numbers, separated by spaces, to a comma-separated format. Combining split () and join (), we can write: [Example] str_speeds = “38 42 20 40 39” speeds = str_speeds.split() csep_speeds = “,”.join(speeds) csep_speeds ↓ 38,42,20,40,39

If you just want to replace the comma with a space, use the string type replase () method [str_speeds.replase (“,“, ”)] However, the combination of split () and join () has the advantage that the replase () method does not have. The split () method can be used even if there are unnecessary spaces before and after the string to be split, or the space between them is complicated. It divides the elements well.

Comparison between using replase () and split () and join () [In the case of replace ()] str_speed2 = “ 38 42 20 40 39 “ str_speed2.replase(“ “, “,”) ↓ ,38,42,20,40,39,

[For split () and join ()] str_speed2 = “ 38 42 20 40 39 “ speed2 = str_speed2.split() csep_speed2 = “,”.join(speed2) ↓ 38,42,20,40,39

Data entered manually tends to have extra spaces like this example. It is convenient to use split () and join () to handle such data.

Recommended Posts

Replace and delete strings
3-3, Python strings and character codes
Replace / delete special symbols with sed
Ansible Jinja2 filters Replace and extract variable strings with regular expressions
[Python] Various combinations of strings and values