Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply Python case

2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is to share with you about how to use Python case. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Convert Python case

Like other languages, Python provides a case conversion method for string objects: upper () and lower (). Not only that, Python also provides us with the capitalize () method for uppercase, the rest for lowercase, and the title () method for all words with uppercase and other lowercase. The function is relatively simple, take a look at the following example:

S = 'hEllo pYthon' print s.upper () print s.lower () print s.capitalize () print s.title ()

Output result:

HELLO PYTHON hello python Hello python Hello Python

Determine the case of Python

Python provides isupper (), islower (), and istitle () methods to determine the case of a string. Note that:

1. The iscapitalize () method is not provided, so we will implement it ourselves, but it is not clear why Python is not implemented for us.

two。 If you use isupper (), islower (), istitle () on an empty string, the result returned is False.

Print 'A'.isupper () # True print' A'.islower () # False print 'Python Is So Good'.istitle () # True # print' Dont do thatpacking room.iscapitalize () # error, there is no iscapitalize () method

Implement iscapitalize

1. If we simply compare the original string with the string converted by capitallize (), if the original string we pass in is an empty string, the return result will be True, which is not consistent with the second point we mentioned above.

Def iscapitalized (s): return s = = s.capitalize ()

Someone thinks of adding a condition when returning and judging that len (s) > 0, which is actually problematic, because when we call iscapitalize ('123'), we return True, not the result we expected.

two。 Therefore, we recall the previous translate method to determine whether the string contains any English letters. The implementation is as follows:

Import string

Notrans = string.maketrans (',')

Def containsAny (str, strset):

Return len (strset)! = len (strset.translate (notrans, str))

Def iscapitalized (s):

Return s = = s.capitalize () and containsAny (s, string.letters)

# return s = = s.capitalize () and len (s) > 0 # if s is a string of numbers

This method will not work. Try calling it:

Print iscapitalized ('123')

Print iscapitalized ('')

Print iscapitalized ('Evergreen is zcr1985')

Output result:

False False True, thank you for your reading! This is the end of the article on "how to apply Python case". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report