zoukankan      html  css  js  c++  java
  • Python学习笔记字符串操作之upper()、lower()、isipper()和islower()方法

     随笔记录方便自己和同路人查阅。

    #------------------------------------------------我是可耻的分割线-------------------------------------------

      一些字符串方法会分析字符串,或生成转变过的字符串。upper()和lower()字符串方法返回一个新字符串,

    其中原字符串的所有字母都被相应地转换为大写或小写。字符串中非字母字符保持不变。

      isupper()和islower()字符串方法判断字符串至少有一个字母,并且所有字母都是大写或小写,isupper()和

    islower()方法就会相应地返回布尔值 True。否则,该方法返回 False。

    #------------------------------------------------我是可耻的分割线-------------------------------------------

      1、upper()方法,示例代码:

    #
    # -*- coding:utf-8 -*-
    # Autor: Li Rong Yang
    spam = 'hello world!'
    spam = spam.upper()
    print(spam)
    

      运行结果:

      2、lower()方法,示例代码:

    #
    # -*- coding:utf-8 -*-
    # Autor: Li Rong Yang
    spam = 'HELLO WORLD!'
    spam = spam.lower()
    print(spam)
    

      运行结果:

      3、islower()方法,示例代码:

    spam = 'HELLO WORLD!'
    spam = spam.islower()
    print(spam)
    
    spam = 'HELLO world!'
    spam = spam.islower()
    print(spam)
    
    spam = 'hello world!'
    spam = spam.islower()
    print(spam)
    

      运行结果:

      根据运行结果可以看出,使用islower()方法时,只有字符串全部为小写才会True,否则为False。

      4、isupper()方法,示例代码:

    #
    # -*- coding:utf-8 -*-
    # Autor: Li Rong Yang
    spam = 'hello world!'
    spam = spam.isupper()
    print(spam)
    
    spam = 'HELLO world!'
    spam = spam.isupper()
    print(spam)
    
    spam = 'HELLO WORLD!'
    spam = spam.isupper()
    print(spam)
    

      运行结果:

      根据运行结果可以看出,使用isupper()方法时,只有字符串全部为大写才会True,否则为False。

       

  • 相关阅读:
    POJ 1703 Find them, Catch them
    POJ 2236 Wireless Network
    POJ 2010 Moo University
    POJ 2184 Cow Exhibition
    POJ 3280 Cheapest Palindrome
    POJ 3009 Curling 2.0
    POJ 3669 Meteor Shower
    POJ 2718 Smallest Difference
    POJ 3187 Backward Digit Sums
    POJ 3050 Hopscotch
  • 原文地址:https://www.cnblogs.com/lirongyang/p/9556885.html
Copyright © 2011-2022 走看看