python_bomb----字符串操作

字节探雪人
• 阅读 1236

字符串的创建

由单引号、双引号、及三层引号括起来的字符

    str = 'hello,sheen'
    str = "hello,sheen"
    str = """hello,sheen"""    #三层引号可输出内容的特定格式

转义字符

一个反斜线加一个单一字符可以表示一个特殊字符,通常是不可打印的字符

/t    =    'tab',/n    =    '换行',/"    =    '双引号本身'

占位字符

| %d | 整数 |
| %f | 浮点数 |
| %s | 字符串 |
| %x | 十六进制整数 |

字符串的特性

索引

>>> h[0]    #正向索引从0开始
'h'
>>> h[-1]    #反向索引从-1开始
'o'

切片

s[start:end:step]   # 从start开始到end-1结束, 步长为step;
    - 如果start省略, 则从头开始切片;
    - 如果end省略, 一直切片到字符串最后;
s[1:]
s[:-1]
s[::-1]    # 对于字符串进行反转
s[:]         # 对于字符串拷贝

python_bomb----字符串操作

成员操作符

in | not in
>>> 'o' in s
True
>>> 'a' in s
False
>>> 'a' not in s
True

连接

a = 'hello'
b='sheenstar'
print("%s %s" %(a,b))
hello sheenstar
a+b
'hellosheenstar'
a+' '+b
'hello sheenstar'

重复

print('*'*20+a+' '+b+"*"*20)
********************hello sheenstar********************

字符串常用方法

大小写

'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper'
'lower', 'upper', 'title'

'Hello'.istitle()    #判断是否是标题
True
'780abc'.isalnum()    #判断是否是数字或字母
True
'780'.isdigit()    #判断是否是数字
True
'abd'.isalpha()    #判断是否是字母
True
'abd'.upper()    #转换为大写
'ABD'
'ADE'.lower()    #转换为小写
'ade'
'sheenSTAR'.swapcase()
'SHEENstar'

开头和结尾匹配

endswith
startswith

    name = "yum.repo"
    if name.endswith('repo'):
        print(name)
    else:
        print("error")
        yum.repo

去掉左右两边空格

strip
lstrip
rstrip
注意: 去除左右两边的空格, 空格为广义的空格, 包括: n, t, r

    >h = '   hello   '
    >h.strip()
    'hello'
    >h
    '   hello   '
    >h.rstrip()
    '   hello'
    >h
    '   hello   '
    >h.lstrip()
    'hello   '

搜索和替换

find:搜索
replace:替换
count:出现次数

    >>> h = "hello sheen .hello python"
    >>> h.find("sheen")
    6
    >>> h.rfind("sheen")    #从右侧开始找,输出仍为正向索引值
    6
    >>> h.rfind("python")
    19
    >>> h.replace("sheen",'star')
    'hello star .hello python'
    >>> h
    'hello sheen .hello python'
    >>> h.count('hello')
    2
    >>> h.count('e')
    4

分离与拼接

split:分离
join:拼接

    >>> date = "2018/08/11"
    >>> date.split('/')
    ['2018', '08', '11']
    >>> type(date.split('/'))
    <class 'list'>
    >>>list=["1","3","5","7"]
    >>>"+".join(list)    
    '1+3+5+7'
点赞
收藏
评论区
推荐文章
美凌格栋栋酱 美凌格栋栋酱
6个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
九路 九路
4年前
Go语言字符串和数值转换
一.字符串概述字符串是一段不可变的字符序列.内容是任意内容,可以是一段文字也可以是一串数字,但是字符串类型数字不能进行数学运算,必须转换成整型或浮点型字符串类型关键字:string创建字符串类型变量govarsstring"hello,world"s1:"hello,world"字符串类型的值使用双引号""扩上
Stella981 Stella981
3年前
Ruby 里的 %Q, %q, %W, %w, %x, %r, %s, %i (译)
%Q用于替代双引号的字符串.当你需要在字符串里放入很多引号时候,可以直接用下面方法而不需要在引号前逐个添加反斜杠(\\") %Q(Joe said: "Frank said: "{what_frank_said}"") "Joe said: "Frank said: "Hello!"""(...)
Stella981 Stella981
3年前
Python format 用法详解
一、填充字符串1\.位置print("hello{0},thisis{1}.".format("world","python"))根据位置下标进行填充print("hello{},thisis{}.".format("world","python"))根据顺序自动填充
Stella981 Stella981
3年前
JavaScript常用函数
1\.字符串长度截取functioncutstr(str,len){vartemp,icount0,patrn/^\x00\xff/,strre"";for(vari
Wesley13 Wesley13
3年前
10条PHP编程习惯助你找工作
http://blog.csdn.net/yihan1029/article/details/417664131、使用单引号括起来的字符串当使用双引号来括字符串时,PHP(https://www.oschina.net/p/php)解释器会对其进行变量替换、转义等操作,如“\\n”。如果只想输出一个基本的字符串,用单引号会节
Wesley13 Wesley13
3年前
Java 基础语法
常量:在程序运行期间,固定不变的量常量的分类:1.字符串常量:凡是用双引号引起来的部分,叫做字符串常量,例如:"abc","hello","123"2.整数常量:直接写上的数字,没有小数点,例如:100,200,0,2503.浮点数常量:直接写上的数字,有小数点,
小万哥 小万哥
12个月前
Kotlin 字符串教程:深入理解与使用技巧
Kotlin中的字符串用于存储文本,定义时使用双引号包围字符序列,如vargreeting&quot;Hello&quot;。Kotlin能自动推断变量类型,但在未初始化时需显式指定类型,如varname:String。可通过索引访问字符串元素,如txt0获取首字符。字符串作为对象,拥有属性和方法,如length获取长度,toUpperCase()转大写。可使用compareTo()比较字符串,indexOf()查找子串位置。字符串中嵌入单引号表示文本内的引号,如&quot;It&39;salright&quot;。使用或plus()
小万哥 小万哥
1年前
C# 字符串操作指南:长度、连接、插值、特殊字符和实用方法
字符串用于存储文本。一个字符串变量包含由双引号括起的字符集合示例:csharp//创建一个string类型的变量并赋予一个值stringgreeting"Hello";如果需要,一个字符串变量可以包含多个单词:示例:csharpstringgreeting
小万哥 小万哥
1年前
Python 中的字符串基础与应用
在Python中,字符串可以用单引号或双引号括起来。'hello'与"hello"是相同的。您可以使用print()函数显示字符串文字:示例:Pythonprint("Hello")print('Hello')将字符串分配给变量是通过变量名后跟等号和字符串
小万哥 小万哥
1年前
C++ 字符串完全指南:学习基础知识到掌握高级应用技巧
C字符串字符串用于存储文本。一个字符串变量包含由双引号括起来的一组字符:示例创建一个string类型的变量并为其赋值:cppstringgreeting"Hello";C字符串连接字符串连接可以使用运算符来实现,生成一个新的字符串。示例:cpps