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

    一、字符串的定义

    python3中字符串使用英文的双引号或单引号定义,如下:

    string = "Hello world"

    或string = 'Hello world'

    二、字符串的输入

    string = input("请输入一个字符串:")

    三、字符串的输出

    print("请打印出string=%s"%string)

    四、字符串的常用操作

      1 #定义一个字符串
      2 string = "Try to be a man what you want to be!"
      3 str = "Youthisastateofmind"
      4 str0 = "12345"
      5 str3 = "   to be    "
      6 
      7 #字符串遍历
      8 for i in string:
      9     print(i)
     10 
     11 #取值
     12 print(string[0])          #取出第一个字符
     13 print(string[0:6])        #取出第一到六位的字符
     14 print(string[::2])        #取出所有字符,步长为2
     15 print(string[-1])         #取出最后一位字符
     16 print(string[-1::2])      #逆序取出字符,步长为2
     17 
     18 #字符串内建函数
     19 #切片,结果为列表
     20 print(string.split(" "))        #以空格切片(即遇到一个空格断开一次)
     21 print(string.split(" ",2))      #以空格切片,仅切两次
     22 print(string.split("be"))       #以be切片(遇到一个be断开一次)
     23 
     24 #字符串首字母大写
     25 print(string.capitalize())
     26 
     27 #字符串每个单词首字母大写
     28 print(string.title())
     29 
     30 #字符串中所有字母小写
     31 print(string.lower())
     32 
     33 #字符串中所有字母大写
     34 print(string.upper())
     35 
     36 #检测字符串中是否包含某段字符(从左往右检索)
     37 print(string.find("be"))           #包含,返回值为从左往右第一次出现的下标值,不包含返回-1
     38 print(string.index("be"))          #包含,返回值为从左往右第一次出现的下标值,不包含报错
     39 print(string.find("be",0,6))       #检索一到六位内是否包含be
     40 
     41 #检测字符串中是否包含某段字符(从右往左检索)
     42 print(string.rfind("be"))          #包含,返回值为从右往左第一次出现的下标值,不包含返回-1
     43 print(string.rindex("be"))         #包含,返回值为从右往左第一次出现的下标值,不包含报错
     44 
     45 #某段字符在字符串中出现的次数
     46 print(string.count("be"))
     47 print(string.count("be",0,10))     #be在一到十位中出现的次数
     48 
     49 #替换字符串中的某段字符
     50 print(string.replace("be","BE"))
     51 print(string.replace("be","BE",1)) #仅替换从左往右第一次出现的be
     52 
     53 #判断字符串是否以某一段字符开头
     54 print(string.startswith("try"))    #返回值为True、False,区分大小写
     55 
     56 #判断字符串是否以某一段字符结尾
     57 print(string.endswith("be"))    #返回值为True、False,区分大小写
     58 
     59 #将一个字符串插入另一个字符串的每个字符处
     60 str1 = "_"
     61 print(str1.join(string))
     62 
     63 #返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串
     64 print(string.ljust(50))            #此处width为50
     65 
     66 #返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串
     67 print(string.rjust(50))
     68 
     69 #返回一个原字符串居中,并使用空格填充至长度 width 的新字符串
     70 print(string.center(50))
     71 
     72 #删除字符串左边空白
     73 print(str3.lstrip())
     74 
     75 #删除字符串右边空白
     76 print(str3.rstrip())
     77 
     78 #删除字符串两端空白
     79 print(str3.strip())
     80 
     81 #判断字符串中字符是否为纯字母或纯数字(仅为字母或仅为数字)
     82 print(string.isalnum())            #返回值为True或False,有空格返回False
     83 print(str.isalnum())
     84 print(str0.isalnum())
     85 
     86 #判断字符串中字符是否为纯字母(仅为字母)
     87 print(string.isalpha())            #返回值为True或False,有空格返回False
     88 print(str.isalpha())
     89 print(str0.isalpha())
     90 
     91 #判断字符串中字符是否为纯数字(仅为数字)
     92 print(string.isdigit())            #返回值为True或False,有空格返回False
     93 print(str.isdigit())
     94 print(str0.isdigit())
     95 
     96 #断字符串中字符是否仅为空格
     97 str2 = "     "
     98 print(string.isspace())            #返回值为True或False
     99 print(str.isspace())
    100 print(str0.isspace())
    101 print(str2.isspace())
    102 
    103 #把字符串分成一个3元素的元组(从左往右)
    104 print(string.partition("be"))        #从左往右第一个be之前,be,be之后三部分
    105 
    106 #把字符串分成一个3元素的元组(从右往左)
    107 print(string.rpartition("be"))        #从右往左第一个be之前,be,be之后三部分

     上方代码输出结果:

    C:Python36-32python.exe D:/PycharmProjects/Python_study/Practice_001/practuce002.py
    **************************************************
    T
    r
    y
     
    t
    o
     
    b
    e
     
    a
     
    m
    a
    n
     
    w
    h
    a
    t
     
    y
    o
    u
     
    w
    a
    n
    t
     
    t
    o
     
    b
    e
    !
    **************************************************
    T
    Try to
    Tyt eamnwa o att e
    !
    !
    **************************************************
    ['Try', 'to', 'be', 'a', 'man', 'what', 'you', 'want', 'to', 'be!']
    ['Try', 'to', 'be a man what you want to be!']
    ['Try to ', ' a man what you want to ', '!']
    **************************************************
    Try to be a man what you want to be!
    **************************************************
    Try To Be A Man What You Want To Be!
    **************************************************
    try to be a man what you want to be!
    **************************************************
    TRY TO BE A MAN WHAT YOU WANT TO BE!
    **************************************************
    7
    7
    -1
    **************************************************
    33
    33
    **************************************************
    2
    1
    **************************************************
    Try to BE a man what you want to BE!
    Try to BE a man what you want to be!
    **************************************************
    False
    **************************************************
    False
    **************************************************
    T_r_y_ _t_o_ _b_e_ _a_ _m_a_n_ _w_h_a_t_ _y_o_u_ _w_a_n_t_ _t_o_ _b_e_!
    **************************************************
    Try to be a man what you want to be!              
    **************************************************
                  Try to be a man what you want to be!
    **************************************************
           Try to be a man what you want to be!       
    **************************************************
    to be    
    **************************************************
       to be
    **************************************************
    to be
    **************************************************
    False
    True
    True
    **************************************************
    False
    True
    False
    **************************************************
    False
    False
    True
    **************************************************
    False
    False
    False
    True
    **************************************************
    ('Try to ', 'be', ' a man what you want to be!')
    **************************************************
    ('Try to be a man what you want to ', 'be', '!')
    
    Process finished with exit code 0
  • 相关阅读:
    Windows 下完全卸载 oracle 10g
    WINDOWS SERVER 工作笔记
    白话 WPF/SL 绑定(Binding) (上)
    系统架构整理笔记待续
    在VMware Workstation 中添加硬盘镜像(*.vmdk)
    HTML+CSS 工作笔记
    用Paragon Partition Manager 7.0 给Windows Server 2003 C盘增加空间
    磁盘分区合并增容(WIN 7, XP)
    Oracle 11G Client 客户端安装步骤(图文详解)
    snk
  • 原文地址:https://www.cnblogs.com/kakaln/p/8001502.html
Copyright © 2011-2022 走看看