zoukankan      html  css  js  c++  java
  • python之路-字符串

    一.类型转换

    a = 10
    print(type(a)) # <class 'int'>
    d = str(a) # 把数字转换成str
    print(type(d)) # <class 'str'>
    b = "10"
    print(type(b)) # <class 'str'>
    c = int(b) # b扔到int() 得到的结果就是一个int
    print(type(c)) # <class 'int'>
    字符串 => 数字   int()
    数字 = > 字符串  str()
    x => y类型  y(x)
    结论一: 想把xxx数据转化成yy类型的数据. yy()

    二. 布尔值

    复制代码
    把数字转化成bool
    0是False, 非零是True
    a = 10
    print(bool(a)) # True
    print(bool(1)) # True
    print(bool(0)) # False
    print(bool(-1)) # True
    复制代码
    空字符串 表示False
    print(bool("哈哈")) # True
    print(bool(" ")) # 空格是True
    print(bool("")) # 空字符串是false
    结论二: 所有的空都可以表示False
    print(bool([])) # False 空列表
    print(bool({})) # False 空字典
    print(bool(set())) # False 空集合

    三 .字符串一些简单操作

      取下标

    复制代码
     s = "今天中午吃胡辣汤"
    
    索引使用[下标]获取数据
    print(s[3])
    print(s[2])
    
    print(s[-3]) # 到着查数,最后一个是 -1
    print(s[-6])
    复制代码

      切片

    复制代码
    # 切片, 从一个字符串中截取出一部分字符串
    # [start: end] 顾头不顾尾   end取不到
    s = "中间的,你们为什么不说话.难受"
    print(s[3:7]) # ,你们为
    print(s[5:9]) # 们为什么
    print(s[-3: -7]) # 切不到东西, 默认是从左往右切
    print(s[-7: -3])  # 么不说话
    print(s[:6]) # 从头开始切
    print(s[6:]) # 切到末尾
    print(s[:])  # 从开始到结束
    复制代码

      步长

    复制代码
    # 步长step,  默认是1 每多少个取一个
    [start: end: step]
    s = "abcdefghijklmn"
    print(s[::2])
    print(s[1:5:3])
    print(s[7:3]) # 默认步长1 从左往右切
    print(s[7:3:-1]) # 从右往左切
    step可以控制方向. 如果step是正数. 从左往右切. 如果是负数 . 从右往左切
    print(s[-1:-8: -2]
    复制代码

      字符串相关操作

    复制代码
    1, upper() 转换成大写. 忽略大小写
    2, strip() 去掉左右两端的空白 空格, 	 
    . 所有用户输入的内容都要去空白
    3, replace(old, new) 把old替换成new
    4, split() 字符串切割
    5, startswith() 判断是否以xxx开头
    6, find() 查找, 找不到返回-1
    7, isdigit() 判断是否是数字组成
    8, len() 求长度
    复制代码

      

    复制代码
    name = '  aleX leNb   '
    print(name.strip())  #去掉空格
    print(name.startswith('al'))
    print(name.replace('l','p'))
    print(name.upper())
    print(name.split('l'))
    print(name.find('e'))
    复制代码
  • 相关阅读:
    JavaScript模态对话框类
    事件模块的演变(1)
    html5中可通过document.head获取head元素
    How to search for just a specific file type in Visual Studio code?
    What do 'lazy' and 'greedy' mean in the context of regular expressions?
    正则非获取匹配 Lookahead and Lookbehind ZeroLength Assertions
    regex length 正则长度问题
    Inversion of Control vs Dependency Injection
    How to return View with QueryString in ASP.NET MVC 2?
    今天才发现Google Reader
  • 原文地址:https://www.cnblogs.com/uiys/p/10672995.html
Copyright © 2011-2022 走看看