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

    字符串

    s="abcdef"
    字符串删除
    del s 

    访问字符串的值

    print(s[0])#a
    print(s[1:5])#bcde
    print(s[::-1])#fedcba
    print(s[:-1])#abcde

    字符串更新

    s="hello"
    s=s[:5]+'world'
    print(s)#helloworld

    字符串运算符

    a="abc"
    b="def"
    字符串连接+
    print(a+b)#abcdef
    字符串重复输出
    print(a*2)#abcabc
    成员运算符in,not in
    print('b' in a)#True
    print('d' in a)#False
    显示原始字符串r或者R
    print(r"abc
    ")#abc
    
    print(R"abc
    ")#abc
    

    字符串内建函数

    1.capitalize()将字符串的第一个字符变大写
    s="hello world"
    print(s.capitalize())#Hello world
    
    2.center(width, fillchar)返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格
    s="abc"
    print(s.center(10,'*'))#***abc****
    
    3.count(str)返回字符出现的次数
    s="aabbcde"
    print(s.count('a'))#2
    
    4.endswith()检查字符串以什么结束,与之对应的是startswith()
    s="abc"
    print(s.endswith('c'))#True
    print(s.endswith('bc'))#True
    
    5.index(str)检测str是否包含在字符串里,返回索引值,不在就报错
    s="hello"
    print(s.index('el'))#1
    
    6.find(str)检测str是否包含在字符串里,返回索引值,不在,就返回-1
    s="hello"
    print(s.find('p'))#-1
    
    7.len()返回字符串长度
    s='hello'
    print(len(s))#5
    
    8.lstrip()截掉字符串左边的空格或指定字符,strip()执行lstrip()和rstrip()
    s="  hello"
    print(s.lstrip())#hello
    s = "hhhello"
    print(s.lstrip('h'))#ello
    
    9.title()所有单词首字母大写
    s="hello world"
    print(s.title())#Hello World
    
    10.swapcase()将字符串中大写转换为小写,小写转换为大写
    s="hello Wo"
    print(s.swapcase())#HELLO wO
    ...................
  • 相关阅读:
    使用supervisor做进程控制
    HDU 4272 LianLianKan [状态压缩DP]
    UVALive 4297 First Knight [期望+高斯消元(优化)]
    HDU 4269 Defend Jian Ge [模拟]
    POJ 2497 Widget Factory [高斯消元解同余方程组]
    HDU 2996 In case of failure [KD树]
    HDU 4268 Alice and Bob [贪心]
    HDU 4273 Rescue [三维几何]
    HDU 4267 A Simple Problem with Integers [树状数组]
    HDU 4271 Find Black Hand [最短编辑距离DP]
  • 原文地址:https://www.cnblogs.com/my12/p/9347328.html
Copyright © 2011-2022 走看看