python字符串基本操作,比如字符串的替换、删除、截取、复制、连接、分割等。都是一些关于字符串的一些方法。下面来列举一些,相信对学习python还是有些帮助的。

1.去除空格--strp();

>>> a="   winner   "
>>> a
' winner '
>>> b=a.strip()
>>> print(b)
winner

还可以使用lstrip()或者rstrip()来删除左边或者右边的空格

>>> a='   winner   '
>>> a
' winner '
>>> a.lstrip()
'winner '
>>> a.rstrip()
' winner'
>>>

2.字符串的复制

可以使用*运算符

>>> a="python"
>>> b=a*5
>>> print(b)
pythonpythonpythonpythonpython

 也可以使用循环语句

>>> str1='and one '
>>> for i in range(5):
print(str1) and one
and one
and one
and one
and one

3.连接字符串

用+连接 

>>> a='this '
>>> b='is '
>>> c='why '
>>> d='we '
>>> e='play'
>>> print(a+b+c+d+e)
this is why we play

4.查找字符串

str.index()方法:检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内。

语法:str.index(str, beg=0, end=len(string))                 str---指定检索的字符串;      beg--开始索引,默认值为0;  end--结束索引,默认为字符串的长度。

返回值:如果包含子字符串返回开始的索引值,否则抛出异常。

 

>>> a='hello world'
>>> a.index('l') >>> a.index('b')
Traceback (most recent call last):
File "<pyshell#2>", line , in <module>
a.index('b')
ValueError: substring not found

str.find()方法:

str.find(str, beg=0, end=len(string))
返回值:如果包含子字符串返回开始的索引值,否则返回-1。
>>> a='hello world'
>>> a.find('w')
6
>>> a.find('c')
-1
5.是否包含指定字符串
in |not in
>>> a='hello world'
>>> 'hello' in a
True
>>> 'hi' in a
False
>>> '' not in a
True
6.字符串长度。
str.len
>>> a='world i am watching you'
>>> print(len(a))
23
7.字符串中字母大小写转换
str.lower()转换为小写。
>>> a='PYTHON'
>>> print(a.lower())
python
 str.upper()转换为大写
>>> b='kingdom'
>>> print(b.upper())
KINGDOM
str.swapcase()大小写互换
>>> c='Only One'
>>> print(c.swapcase())
oNLY oNE
str.capitalize()首字母大写 
>>> d='hello world'
>>> print(d.capitalize())
Hello world

8.将字符串放入中心位置,以及两边字符的修饰。

str.center(width[, fillchar])     width--字符串的总宽度;fillchar--填充字符
>>> a="欢迎!"
>>> print(a.center(40,'*'))
******************欢迎!*******************
9.字符串统计
str.count()
>>> a='sauwiqjaiwaa'
>>> print(a.count('a'))
4
10.字符串切片
 
>>> a="你的魔法也救不了你"
>>> print(a[:-1])
你的魔法也救不了
>>>
>>> print(a[0:3])
你的魔

相关练习;

1.定义两个字符串分别为 xyz 、abc

2.对两个字符串进行连接

3.取出xyz字符串的第二个和第三个元素

4.对abc输出10次

5.判断a字符(串)在 xyz 和 abc 两个字符串中是否存在,并进行输出

>>> m='xyz'
>>> n='abc'
>>> print(m+n)
xyzabc
>>> print(m[1:])
yz
>>> for i in range(10):
print(n) abc
abc
abc
abc
abc
abc
abc
abc
abc
abc
>>> 'a' in m
False
>>> 'a' in n
True

关于python字符串基本操作的更多相关文章

  1. Python 字符串基本操作

    字符串是Python的一种基本类型,字符串的操作包括字符串格式化输出.字符串的截取.合并,字符串的查找和替换等操作. 字符串定义 Python中有3种表示字符串的方法:单引号.双引号.三引号.引号使用 ...

  2. python字符串基本操作

  3. 3. python 字符串的一般使用

    3. python 字符串的一般使用 1.基本操作 1)使用+连接 >>> "abc"+"efg"    'abcefg'    >&g ...

  4. Python SQLAlchemy基本操作和常用技巧包含大量实例,非常好python

    http://www.makaidong.com/%E8%84%9A%E6%9C%AC%E4%B9%8B%E5%AE%B6/28053.shtml "Python SQLAlchemy基本操 ...

  5. Python 字符串——巧取值和列表——巧取值 对比

    Python 字符串——巧取值和列表——巧取值 对比 1.字符串取值实例: samp_string = "Whatever you are, be a good one." for ...

  6. 关于python字符串连接的操作

    python字符串连接的N种方式 注:本文转自http://www.cnblogs.com/dream397/p/3925436.html 这是一篇不错的文章 故转 python中有很多字符串连接方式 ...

  7. StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?

    StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...

  8. Python 字符串

    Python访问字符串中的值 Python不支持单字符类型,单字符也在Python也是作为一个字符串使用. Python访问子字符串,可以使用方括号来截取字符串,如下实例: #!/usr/bin/py ...

  9. python字符串方法的简单使用

    学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...

随机推荐

  1. POJ 1410 Intersection (线段和矩形相交)

    题目: Description You are to write a program that has to decide whether a given line segment intersect ...

  2. hiho 1098 最小生成树二·Kruscal算法 (最小生成树)

    题目: 时间限制:10000ms 单点时限:1000ms 内存限制:256MB   描述 随着小Hi拥有城市数目的增加,在之间所使用的Prim算法已经无法继续使用了——但是幸运的是,经过计算机的分析, ...

  3. 解决nginx和php使用ckfinder无法上传大文件的问题

    现象描述:cms内容发布系统上传不了大文件,当上传超过32M文件时就上传不了 提示:无效的文件. 文件尺寸太大. 分析文件上传过程:browser --> nginx --> php 需要 ...

  4. 关于button去掉自带阴影效果的方法

    在button的属性设置里加上: style=”?android:attr/borderlessButtonStyle” 即: <Button android:layout_width=&quo ...

  5. 【原创】大叔算法分享(5)聚类算法DBSCAN

    一 简介 DBSCAN:Density-based spatial clustering of applications with noise is a data clustering algorit ...

  6. @Autowired mapper 层次 bean 带红线

    在利用@Autowired 注解创建bean 时候 有时间会带有下滑红色横线 给人一种报错的感觉 下面是去除红线的办法 将颜色红色error 等级降低为黄色warn  即可

  7. uni-app hbuilderX ios离线打包,启动图修改没反应

    最后还是没反应 删除app从新安装, 可参考https://www.jianshu.com/p/47c1377c61d6

  8. django 实战篇之视图层

    视图层(views.py) django必会三板斧 HttpResponse >>> 返回字符串 render >>> 支持模板语法,渲染页面,并返回给前端 red ...

  9. 5、Filebeat工作原理

    Filebeat工作原理 Filebeat由两个主要组件组成:inputs和harvesters. 这些组件协同工作来查看最新文件内容并将事件数据发送到指定的输出.(注意与之前版本的不同,之前版本是p ...

  10. Excel-VBA入门(1): 基础 / 变量 /for / if/ 调试

    (一) 启动VBA 打开excel ,选项-自定义功能区-开发工具, 在界面的开发工具下选择 宏安全:  勾选 启用所有 excel保存xlsm后缀的格式才可以用宏! 若启动VBA编辑器(以下简称VB ...