Python字符串 casefold() 方法用于实现无大小写字符串匹配。类似于 下() 字符串方法,但大小写 删除所有大小写差异 以一串的形式出现。i、 e在比较时忽略案例。
null
语法:
一串casefold()
参数:
casefold()方法不接受任何参数。
返回值:
它返回大小写折叠字符串,并将字符串转换为小写。
示例1:转换小写字符串
Python3
# Python program to convert string in lower case string = "GEEKSFORGEEKS" # print lowercase string print ( "lowercase string: " ,string.casefold()) |
输出:
lowercase string: geeksforgeeks
示例2:检查字符串是否为回文
Python3
# Program to check if a string # is palindrome or not # change this value for a different output str = 'geeksforgeeks' # make it suitable for caseless comparison str = str .casefold() # reverse the string rev_str = reversed ( str ) # check if the string is equal to its reverse if str = = rev_str: print ( "palindrome" ) else : print ( "not palindrome" ) |
输出:
not palindrome
例3:计算字符串中的元音
Python3
# Program to count # the number of each # vowel in a string # string of vowels v = 'aeiou' # change this value for a different result str = 'Hello, have you try geeksforgeeks?' # input from the user # str = input("Enter a string: ") # caseless comparisions str = str .casefold() # make a dictionary with # each vowel a key and value 0 c = {}.fromkeys(v, 0 ) # count the vowels for char in str : if char in c: c[char] + = 1 print (c) |
输出:
{'a': 1, 'e': 6, 'i': 0, 'o': 3, 'u': 1}
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END