A simple example of an Python string case conversion

  • 2020-05-24 05:46:07
  • OfStack

All letters are converted to uppercase

# -*- coding:utf-8 -*-

if __name__ == "__main__":
a = 'hello, world!'
print (a upper ()) output:


HELLO, WORLD!

All the letters are converted to lowercase

# -*- coding:utf-8 -*-

if __name__ == "__main__":
a = 'HELLO, WORLD!'
print (a lower ()) output:


hello, world!

(3) convert the first letter to uppercase and the rest to lowercase

# -*- coding:utf-8 -*-

if __name__ == "__main__":
a = 'HELLO, WORLD!'
print (a capitalize ()) output:


Hello, world!


The first letter of all the words is converted to uppercase and the rest to lowercase

# -*- coding:utf-8 -*-

if __name__ == "__main__":
a = 'HELLO, WORLD!'
print (a title ()) output:


Hello, World!

All the letters are capitalized

# -*- coding:utf-8 -*-

if __name__ == "__main__":
a = 'HELLO, WORLD!'
print(a.isupper())

b = 'hello, world!'
print (b isupper ()) output:


True
False


Judge that all letters are lowercase

# -*- coding:utf-8 -*-

if __name__ == "__main__":
a = 'HELLO, WORLD!'
print(a.islower())

b = 'hello, world!'
print (b islower ()) output:


False
True


Judge that the first letter of all the words is uppercase

# -*- coding:utf-8 -*-

if __name__ == "__main__":
a = 'HELLO, WORLD!'
print(a.istitle())

b = 'hello, world!'
print(b.istitle())

c = 'Hello, World!'
print (c istitle ()) output:


False
False
True


Related articles: