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

    1、python 字符串(总结)

    字符串的定义:

    在python的用双引号“”或单引号‘’,关闭字符的就是字符串。

    例如:

    x = “小米”(在python应用程序中开辟了一个内存,把小米丢进去,在名称空间里开辟一个空间把x丢进去,绑定小米的内存地址,才能引用它)

    在python中x是变量名 对应着变量值是小米 ,= 等号是赋值的意思,就是把 小米(变量值的内存地址)=(绑定)x(变量名)

    字符串只能存一个值,像姓名

    列举字符串常用的几种用法

    1.索引

    y=x【0】(小)

    2.切片

    y = x[:](小米)

    正向取 y = 【1::1】(米)

    反向取 y = 【-1::1】(米)

    取反

    y = 【1::-1】(米小)

    y = 【-1::-1】(米小)

    3.strip() (去空白,可以在括号里加参数)

    源码

    def strip(self, chars=None): # real signature unknown; restored from __doc__
    """
    S.strip([chars]) -> str

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove characters in chars instead.
    """
    return "" #这里有返回值的

    y = x.strip() (有返回值一定要拿变量接受)

    4.split()分割

    def split(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
    """
    S.split(sep=None, maxsplit=-1) -> list of strings

    Return a list of the words in S, using sep as the
    delimiter string. If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator and empty strings are
    removed from the result.
    """
    return [] 有返回值

    5.upper()(全部变大)lower()(全部变小)
    例如:
    a = "alex"
    b = a.upper()
    b = "ALEX"
     def upper(self): # real signature unknown; restored from __doc__
            """
            S.upper() -> str
            
            Return a copy of S converted to uppercase.
            """
            return ""  有返回值
    

    6.replace (替换)

    1  def replace(self, old, new, count=None): # real signature unknown; restored from __doc__
    2         """
    3         S.replace(old, new[, count]) -> str
    4         
    5         Return a copy of S with all occurrences of substring
    6         old replaced by new.  If the optional argument count is
    7         given, only the first count occurrences are replaced.
    8         """
    9         return ""
    View Code

    x = "alex"

    把ex替换成sb

    b = x.replace('ex',"sb")

    b = 'alsb'

    7.index() 查看 ‘字符串’的字符的索引(也称下标)

    找不到会报错

    对 find()找到就返回1,找不到返回 0

    8.count (子元素,在字符串里出现的次数)

    9.startswith() 以什么开头,返回True 或Flase ,endswith(),返回True或Flase

    10.isdigit()判断字符串能不能转换成整数

    ,如果字符串中出现负数或是小数都是Flase ,只能是正整数

    重点: in  和 not in 成员运算,(以后用的特别多)

    ‘小’ in “小米” 返回值 是 True

    ‘小’ not in ‘小米’ 返回值是False

    循环 (for循环)(while循环)

    用函数封装功能:

    for (循环)

    def str_for (string):

      for i in string:

        print(i)

      return string

    str_for(''python成长中")

    while(循环)

    def str_while(string):

      i = 0

      while i < len(string):

        print(i)

        i += 1

      return string

    str_while(''python成长中")

      

      

      

     
     
  • 相关阅读:
    骆驼命名法
    tftp服务器最简单安装配置
    debian安装后sudo命令不能用的解决方法
    date,datetime,timestamp 的区别
    Linux修改SSH端口和禁止Root远程登陆
    Linux下TFTP的安装,配置和操作
    Linux中find常见用法示例
    做SEO推广必须要做的9件事儿
    网站数据更新
    数据盘分区及挂载新分区
  • 原文地址:https://www.cnblogs.com/wuheng-123/p/9162879.html
Copyright © 2011-2022 走看看