Python-Ref > String manipulation > String basics > Asking about strings content
 
 

<-^^->
Klíčová slova
Moduly
Knihovní funkce

Asking about strings content

How to easily find if a string contains number, capitalized letters etc.
The string object specifies many very useful methods. Several of them may be used to ask if the string contains a particular type of data, such as numbers, capital letters, spaces, etc.
Expand/Shrink
  1   text1 = "234"
  2   text2 = "3.14"
  3   text3 = "hello"
  4   text_long = "There were 42 monkeys in the airplane."
  5   text_spaces = "   \t"
  6   text_title = "String Methods"
  7   
  8   for t in [text1, text2, text3, text_long, text_spaces, text_title]:
  9       print '"%s"' % t
 10       print "Alphanumeric:",t.isalnum()
 11       print "Alphabetic:  ",t.isalpha()
 12       print "Digits:      ",t.isdigit()
 13       print "Lowercase:   ",t.islower()
 14       print "Space:       ",t.isspace()
 15       print "Title:       ",t.istitle()
 16       print "Uppercase:   ",t.isupper()
 17       print
stdout:
"234"
Alphanumeric: True
Alphabetic:   False
Digits:       True
Lowercase:    False
Space:        False
Title:        False
Uppercase:    False

"3.14"
Alphanumeric: False
Alphabetic:   False
Digits:       False
Lowercase:    False
Space:        False
Title:        False
Uppercase:    False

"hello"
Alphanumeric: True
Alphabetic:   True
Digits:       False
Lowercase:    True
Space:        False
Title:        False
Uppercase:    False

"There were 42 monkeys in the airplane."
Alphanumeric: False
Alphabetic:   False
Digits:       False
Lowercase:    False
Space:        False
Title:        False
Uppercase:    False

"   	"
Alphanumeric: False
Alphabetic:   False
Digits:       False
Lowercase:    False
Space:        True
Title:        False
Uppercase:    False

"String Methods"
Alphanumeric: False
Alphabetic:   False
Digits:       False
Lowercase:    False
Space:        False
Title:        True
Uppercase:    False

Doba běhu: 15.6 ms