Python-Ref > String manipulation > String basics > Playing with letter cases
 
 

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

Playing with letter cases

Capitalization, conversion to lowercase and other methods that change the capitalization of a string.
The string object specifies many very useful methods. Several of them may be used to change the capitalization of a string.
Expand/Shrink
  1   text_long = "There were 42 monkeys in the airplane."
  2   text_word = "hello"
  3   text_title = "String Methods"
  4   
  5   for t in [text_long, text_word, text_title]:
  6       print '"%s"' % t
  7       print "Swapcase:   ",t.swapcase()
  8       print "Uppercase:  ",t.upper()
  9       print "Lowercase:  ",t.lower()
 10       print "Title:      ",t.title()
 11       print "Capitalize: ",t.capitalize()
 12       print
stdout:
"There were 42 monkeys in the airplane."
Swapcase:    tHERE WERE 42 MONKEYS IN THE AIRPLANE.
Uppercase:   THERE WERE 42 MONKEYS IN THE AIRPLANE.
Lowercase:   there were 42 monkeys in the airplane.
Title:       There Were 42 Monkeys In The Airplane.
Capitalize:  There were 42 monkeys in the airplane.

"hello"
Swapcase:    HELLO
Uppercase:   HELLO
Lowercase:   hello
Title:       Hello
Capitalize:  Hello

"String Methods"
Swapcase:    sTRING mETHODS
Uppercase:   STRING METHODS
Lowercase:   string methods
Title:       String Methods
Capitalize:  String methods

Doba běhu: 13.7 ms