gpt4 book ai didi

python - 用破折号替换空格并从字符串中删除前缀

转载 作者:太空狗 更新时间:2023-10-30 00:22:04 27 4
gpt4 key购买 nike

我用它来删除空格和特殊字符并将字符转换为小写:

''.join(e for e in artistName if e.isalnum()).lower()

我想:

  • -

    替换空格
  • 如果字符串以单词the开头,那么它

例如,The beatles music! 将变成 beatles-music

最佳答案

artistName = artistName.replace(' ', '-').lower()
if artistName.startswith('the-'):
artistName = artistName[4:]
artistName = ''.join(e for e in artistName if e.isalnum() or e == '-')

关于python - 用破折号替换空格并从字符串中删除前缀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5861361/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com