zoukankan      html  css  js  c++  java
  • 字符串的核心应用

      字符串有2种表示方式:单引号和双引号!他们没有任何区别!比如:

    "python"'python'是一样的。

        但是我觉得组合起来用:

    s = "'你好'"
    h="你好你好'%s'" % s
    特别是sql语句中,直接输出小学会遇到些问题,所以最用'括起来,如果用"就需要转义

      转义:"i love "python""

      拼接:

      最好不要用"+",连接,因为这样连接,每+一次就会在内存在创建一连续的空间,推荐使用格式化字符串,我常用的是:

    "%s%s%s" % ("1","2","3")

       str和repr: str 会把值转成合理的字符串,repr会创建一个字符串。以合法的python表达式来表示值。如:

    print(str("hello world"))
    print(repr("hello world"))
    
    """
    D:pythonpython.exe D:/py1/day1/str.py
    hello world
    'hello world'
    
    Process finished with exit code 0
    """

      input和raw_input

         3.0的input已经取代了2.x的raw_input,他们的意思把用户输入的值转成字符串,2.x的input相当天3.0的eval(input) 相于用户输入什么格式就回什么格式

      

    s = input("请输入:")
    print(type(s))
    print(type(eval(input("再输入"))))

    """

    请输入:10
    <class 'str'>
    再输入10
    <class 'int'>

    多行字符串

    """

    你好

    中国

    大虾

    """

    python3.0中 所有字符串都是unicode字符串

      核心字符串函数:

        find 可以在长字符串中查找字串,如果没有找到则返回-1

    cstr="i love kk ilove python"
    title.find("love")
    cstr="i love kk ilove python"
    cstr.find("kk",0,2)
    find返回并不是布尔值,返回的是子串的位置

      join  它是split的反方法,用来连接序列中的元素:

    s=["1","2","3"]
    print("|".join(s))

      lower  返回字符串的小写

    s="KKDHDSDSDS"
    print(s.lower())

      replace  替换指定功能

    s="KKDHDSDSDS"
    print(s.replace("K","I"))
    
    s="KKDHDSDSDS"
    print(s.replace("K","I",1))
    
    第三个参数指定替换几次,默认-1 是所有都替换

      strip  去掉前后空格

      center 控制输出位置 

    "中国".center(50,"*")

    D:pythonpython.exe D:/py1/day1/str.py
    ************************中国************************

     split  分隔字符串  

    s = "中国|人民|生活"
    print(s.split("|"))
    
    D:pythonpython.exe D:/py1/day1/str.py
    ['中国', '人民', '生活']

    s = "中国|人民|生活"
    a,b,c =s.split("|")
    print(a,b,c)

    """
    D:pythonpython.exe D:/py1/day1/str.py
    中国 人民 生活
    """

      

    
    
  • 相关阅读:
    hdu 4027 Can you answer these queries? 线段树
    ZOJ1610 Count the Colors 线段树
    poj 2528 Mayor's posters 离散化 线段树
    hdu 1599 find the mincost route floyd求最小环
    POJ 2686 Traveling by Stagecoach 状压DP
    POJ 1990 MooFest 树状数组
    POJ 2955 Brackets 区间DP
    lightoj 1422 Halloween Costumes 区间DP
    模板 有源汇上下界最小流 loj117
    模板 有源汇上下界最大流 loj116
  • 原文地址:https://www.cnblogs.com/sunface/p/5164371.html
Copyright © 2011-2022 走看看