When you want to replace multiple characters in a string without using regular expressions in python3 series

When you want to replace multiple characters in a string with python3 series

Instructions / functions / methods to use

sample

sample.py


lst = ['1', '2', '3']
delete_for_str = str.maketrans(dict.fromkeys(lst, "<ok>")) #1st argument:Iterator,2nd argument:Character string after replacement
oldstr = '1ho2ge3'
newstr = oldstr.translate(delete_for_str)
print(newstr)
#Output result:
#<ok>ho<ok>ge<ok>

Recommended Posts

When you want to replace multiple characters in a string without using regular expressions in python3 series
Memorandum Regular expression When there are multiple characters in the character string that you want to separate
When using regular expressions in Python
How to write a string when there are multiple lines in python
I want to embed a variable in a Python string
When you want to plt.save in a for statement
[Linux] When you want to search for a specific character string from multiple files
[Django] A memorandum when you want to communicate asynchronously [Python3]
When you want to hit a UNIX command on Python
[Subprocess] When you want to execute another Python program in Python code
Timezone specification when converting a string to datetime type in python
[Python] When you want to use all variables in another file
If you want to assign csv export to a variable in python
When you want to sort a multidimensional list by multiple lines
When you want to use multiple versions of the same Python library (virtual environment using venv)
Replace non-ASCII with regular expressions in Python
How to use regular expressions in Python
Note: [Python3] Convert datetime to a string in any format you like
When you want to replace a column with a missing value (NaN) column by column
[Python] If you want to draw a scatter plot of multiple clusters
I want to color a part of an Excel string in Python
[Python] What to do if you get a ModuleNotFoundError when importing pandas using Jupyter Notebook in Anaconda
How to embed a variable in a python string
I want to create a window in Python
Python Note: When assigning a value to a string
How to take multiple arguments when doing parallel processing using multiprocessing in python
If you want to display values using choices in a template in a Django model
[Python] What to check when you get a Unicode Decode Error in Django
When you want to erase characters at once in terminal or line unit
[Python3] Code that can be used when you want to cut out an image in a specific size
Parse a JSON string written to a file in Python
How to convert / restore a string with [] in python
I want to easily implement a timeout in python
How to write string concatenation in multiple lines in Python
[Python] How to expand variables in a character string
I want to work with a robot in python.
Things to note when initializing a list in Python
When you want to play a game via Proxy
How to retrieve multiple arrays using slice in python.
How to execute a command using subprocess in Python
I want to do something in Python when I finish
To receive multiple return values ​​from a function executed using parallel processing (Pool) in Python
I know? Data analysis using Python or things you want to use when you want with numpy
[Small story] A painstaking measure when you have to execute a function before import in Python
How to quickly count the frequency of appearance of characters from a character string in Python?
[Python3] Code that can be used when you want to resize images in folder units
[Python] When you want to import and use your own package in the upper directory
When a character string of a certain series is in the Key of the dictionary, the character string is converted to the Value of the dictionary.
If you want to use field names with hyphens when updating firestore data in python
Solution when you want to use cv_bridge with python3 (virtualenv)
How to slice a block multiple array from a multiple array in Python
How to import a file anywhere you like in Python
A simple way to avoid multiple for loops in Python
A memo when creating a directed graph using Graphviz in Python
How to define multiple variables in a python for statement
To return char * in a callback function using ctypes in Python
Try building a neural network in Python without using a library
Things to keep in mind when using Python with AtCoder
I tried to make a stopwatch using tkinter in python
Things to keep in mind when using cgi with python.
I want to make input () a nice complement in python