zoukankan      html  css  js  c++  java
  • Python-字符串

    #字符串全部大写upper()
    value="abcdef"
    new_value=value.upper()
    print(new_value)

    ABCDEF
    #字符串全部小写lower()
    value="ABCEDF"
    new_value=value.lower()
    print(new_value)

    abcedf

    #验证码示例

    check_code='abCD'
    message='请输入验证码%s:' %(check_code)
    code = input(message)
    
    new_check_code=check_code.lower()
    new_code=code.lower()
    
    if new_code == new_check_code:
        print('输入成功')
    #不管输入大写,还是小写都会成功,但是看着很繁琐

    #验证码升级版

    check_code='abCD'
    code=input('请输入验证码%s:'%(check_code))
    if code.lower()==check_code.lower():
        print('登录成功')

    #判断是否为数字 isdigit

    num='abc'
    flag=num.isdigit()
    print(flag)
    False
    
    num='123'
    flag=num.isdigit()
    print(flag)
    True

    #升级版

    num=input('请输入一个数字:')
    flag=num.isdigit()
    if flag:
        print(flag)
    else:
        print('请输入数字...')

    请输入一个数字:asdf
    请输入数字...

    #去除空白lstript   rstript  stript

    #去除右边空格
    user='abcdef        '
    new_user=user.rstrip()
    print(new_user,'------')

    abcdef ------

    #去除左边空格 user=' abcdefg' new_user=user.lstrip() print('------',new_user)

    ------ abcdefg


    #同时去除两边空格 user=' abcdef ' new_user=user.strip() print('------',new_user,'----------')

    ------ abcdef ----------


    #替换字符串replace, 

    message=input('请输入:')
    data=message.replace('你妹的','***')    #将输入你妹的  替换成***
    print(data)

    请输入:你妹的啊
    ***啊

    #只替换前两个

    message=input('请输入:')
    data=message.replace('你妹的','***',2) #2 前两个
    print(data)

    请输入:你妹的 你妹的 你妹的
    *** *** 你妹的

    #切割字符串成为列表  split   rsplit(从右边向左切割)

    #切割
    message='好好学习,天天向上..!!'
    result=message.split(',')   #根据,号切割
    print(result)

    ['好好学习’,’天天向上..!!']


    #只切割第一段
    message='好好学习,天天向上,就不乐意!!'
    result=message.split(',',1)
    print(result)

    ['好好学习', '天天向上,就不乐意!!']




    #计算长度 len

    value="好好学习"
    number=len(value)
    print(number)

    4

    #索引

    #从0开始 0a  1b  2c...
    #获取a
    value='abcedfg'
    v1=value[0]
    print(v1)

    a

    #举个例子  练习

    #输入任意字符串,判断字符串中有多少个数字

    text=input('请输入内容:')
    index_len=len(text)
    index=0
    num=0
    while True:
        val=text[index]
        if val.isdigit():
            num+=1
        if index == index_len -1:
            break
        index +=1
    print(num)

    请输入内容:asdf1234asdf
    4

    #切片

    #从0开始,0:g 1:o 2:0 3:d 4:b 5:o 6:y
    v='goodboy' v1=v[3:5] print(v1)

    db

    v2=v[4:]
    boy



  • 相关阅读:
    bzoj 1030 [JSOI2007]文本生成器
    Swift 学习笔记 (闭包)
    Swift 学习笔记 (函数)
    HTML 学习笔记 JQueryUI(Interactions,Widgets)
    HTML 学习笔记 JQuery(表单,表格 操作)
    HTML 学习笔记 JQuery(animation)
    HTML 学习笔记 JQuery(盒子操作)
    HTML 学习笔记 JQuery(事件)
    HTML 学习笔记 JQuery(DOM 操作3)
    HTML 学习笔记 JQuery(DOM 操作2)
  • 原文地址:https://www.cnblogs.com/sky00747/p/11498415.html
Copyright © 2011-2022 走看看