# 示例代码:删除字符串中指定字符
def remove_specified_chars(s, chars_to_remove):
    """
    删除字符串 s 中的所有指定字符 chars_to_remove。
    参数:
    s (str): 原始字符串。
    chars_to_remove (str): 需要删除的字符组成的字符串。
    返回:
    str: 删除指定字符后的新字符串。
    """
    # 使用字符串的 translate 方法结合 maketrans 创建一个转换表,将指定字符映射为 None
    translation_table = str.maketrans('', '', chars_to_remove)
    return s.translate(translation_table)
# 示例用法
original_string = "hello world!"
chars_to_remove = "lo"
new_string = remove_specified_chars(original_string, chars_to_remove)
print(new_string)  # 输出: he wrd!上一篇:python中::的用法
下一篇:python中的变量
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站