简体   繁体   English

如何在python中比较“ hello:” ==“ hello”

[英]how can I compare “hello:”==“hello” in python

我想忽略字符串中的符号,并与非符号字符串name="Avengers Endgame"

find_element_by_link_text(name.title()).click()
<a href="#">Avengers: Endgame</a>

You could use a regex on the word class ( \\W ). 您可以在单词class( \\W )上使用正则表达式 From the link, 通过链接,

 \\W Matches any character which is not a word character. This is the opposite of \\w. If the ASCII flag is used this becomes the equivalent of [^a-zA-Z0-9_]. If the LOCALE flag is used, matches characters considered alphanumeric in the current locale and the underscore. 

Like, 喜欢,

import re
a = 'Avengers Endgame'
b = 'Avengers: Endgame'
if re.sub(r'[\W]', '', a) == re.sub(r'[\W]', '', b):
    print("They match")

I wrote this function for another project. 我为另一个项目编写了此函数。 Use re module, I guess it can help you. 使用re模块,我想它可以为您提供帮助。

def convert_string(str):
    '''
    return the string without any signs

    ex :
    input = 'abcdefghijklmnopqrstuvwxyz123456789!@#$%^&*()-=_+'
    ouput = 'abcdefghijklmnopqrstuvwxyz123456789'
    '''

    pat = re.compile(r'[^a-zA-Z0-9]', flags=re.IGNORECASE)
    return pat.sub('', str)

you could use a generator expression to only yield characters from your string that are alphanumeric or whitespace, then join them back into a new string: 您可以使用生成器表达式从字符串中仅产生字母数字或空格字符,然后将它们重新组合成新的字符串:

text = 'Avengers: Endgame'
stripped_text = ''.join(char for char in text if char.isalnum() or char.isspace())

your new string would then be: Avengers Endgame 您的新字符串将是: Avengers Endgame

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 运行 Hello world c++ 代码 - How can I run a Hello world c++ code using python 您好,我想知道如何在python中使用Enum - Hello, I would like to know how to use Enum in python 你好,我该如何解决这个驱动程序python错误? - Hello, how do i solve this driver python error? 如何解决Qt Designer中的“ hello world”程序? - How can I troubleshoot an “hello world” program in Qt Designer? 如何在像 hello world 这样的新行中打印 86? - How can I print 86 in new line like hello world? 我如何打印你好和所有女性成员 - How can I print hello and all female members 如何在Manim Hello World程序中修复AttributeError? - How can I fix AttributeError in Manim Hello World program? 您好,请有人解释一下我如何才能制定解决方案以将完整的“Hello World”加密为莫尔斯电码? - Hello there, can pls someone explain me how exactly I can make a solution to get the full "Hello World" encrypted into the morse code? python 打印“你好世界”与“你好世界” - python print "hello world" vs "hello world" 有没有办法启动 python inrepeter,这样我就可以 python print(&#39;hello world&#39;) 而不先按 Enter 键? - Is there a way to start the python intrepeter so i can python print('hello world') without hitting enter first?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM