Python DayDayUp series-string operation (1), pythondayup
This document describes several common string methods.
One String method: upper (), lower (), isupper (), islower ()
The upper () and lower () Methods return a new string. The letters of the original string are converted to uppercase or lowercase letters.
Non-letter characters remain unchanged.
>>> spam = 'Hello world!'>>> spam.upper()'HELLO WORLD!'>>> spam.lower()'hello world!'
If a string contains at least one letter, isupper () and islower (), True or False is returned Based on the upper or lower case letters (both uppercase and lowercase:
>>> Spam = 'Happy tomorrow '>>> spam. islower () False >>> spam. isupper () False >>> spam = '000000' >>> spam. islower () False >>> spam. isupper () False >>> spam = 'Happy '>>> spam. isupper () True
Binary string method isX (return value is True False ):
- Isalpha (): returns True. The string contains only letters.
- Isalnum (): True. The string contains only letters and numbers and is not empty.
- Isdecimal (): True. The string contains only numeric characters and is not empty.
- Isspace (): True. If the string contains only spaces, tabs, and line breaks, it is not empty.
- Istitle (): True. A string can only start with an uppercase letter and is followed by a word with a lowercase letter.
>>> spam = 'hello'>>> spam.isalpha()True>>> spam.isalnum()True
# Test isdecimal () while True: x = input ('enter your age: ') if x. isdecimal (): break print ('Enter the number') # test isalnum () while True: password = input ('Enter the password: ') if password. isalnum (): break print ('password can only be numbers or letters! ')