1. 1. 查看类型
  1. name = 'allen'
  2. print(type(name)) #查看类型
  3. <class 'str'> #类型为str
  4.  
  5. age = 19
  6. print(type(name)) #查看类型
  7. <class 'int'> #类型为int

 2. center

描述:居中显示

语法:

  1. def center(self, width, fillchar=None): # real signature unknown; restored from __doc__
  2. """
  3. B.center(width[, fillchar]) -> copy of B
  4.  
  5. Return B centered in a string of length width. Padding is
  6. done using the specified fill character (default is a space).
  7. """
  8. pass

样例:

  1. name = 'allen'
  2. result=name.center(20,'*') #居中对齐,显示20个字符,默认以*号代替
  3. print(result)
  4.  
  5. *******allen******** #显示结果

center

3. ljust

描述:左对齐显示

语法:

  1. def ljust(self, width, fillchar=None): # real signature unknown; restored from __doc__
  2. """
  3. S.ljust(width[, fillchar]) -> str
  4.  
  5. Return S left-justified in a Unicode string of length width. Padding is
  6. done using the specified fill character (default is a space).
  7. """
  8. return ""

样例:

  1. name = 'allen'
  2. result=name.ljust(20,'*') #左对齐,显示20个字符,默认以*号代替
  3. print(result)
  4.  
  5. allen*************** #显示结果

ljust

 4. rjust

描述:右对齐显示

语法:

  1. def rjust(self, width, fillchar=None): # real signature unknown; restored from __doc__
  2. """
  3. S.rjust(width[, fillchar]) -> str
  4.  
  5. Return S right-justified in a string of length width. Padding is
  6. done using the specified fill character (default is a space).
  7. """
  8. return ""

样例:

  1. name = 'allen'
  2. result=name.rjust(20,'*') #居右对齐,显示20个字符,默认以*号代替
  3. print(result)
  4.  
  5. ***************allen #显示结果

rjust

5. capitalize/title

描述:首字母大写

语法:

  1. def capitalize(self): # real signature unknown; restored from __doc__
  2. """
  3. S.capitalize() -> str
  4.  
  5. Return a capitalized version of S, i.e. make the first character
  6. have upper case and the rest lower case.
  7. """
  8. return ""

样例:

  1. name = 'allen'
  2. result=name.capitalize() #首字母转换为大写,其他小写
  3. print(result)
  4.  
  5. Allen #结果显示
  6.  
  7. name = 'allen'
  8. result=name.title() #首字母转换为大写,其他小写
  9. print(result)
  10.  
  11. Allen #结果显示

capitalize

6. lower

描写:所有字符转换为小写

语法:

  1. def lower(self): # real signature unknown; restored from __doc__
  2. """
  3. S.lower() -> str
  4.  
  5. Return a copy of the string S converted to lowercase.
  6. """
  7. return ""

样例:

  1. name = 'ALLEN'
  2. result=name.lower() #所有字符转换为小写
  3. print(result)
  4.  
  5. allen #结果显示

lower

7. upper

描写:所有字符转换为大写

语法:

  1. def upper(self): # real signature unknown; restored from __doc__
  2. """
  3. S.upper() -> str
  4.  
  5. Return a copy of S converted to uppercase.
  6. """
  7. return ""

样例:

  1. name = 'allen'
  2. result=name.upper() #所有字符转换为大写
  3. print(result)
  4.  
  5. ALLEN #结果显示

upper

8. encode

描写:编码转换

语法:

  1. def encode(self, encoding='utf-8', errors='strict'): # real signature unknown; restored from __doc__
  2. """
  3. S.encode(encoding='utf-8', errors='strict') -> bytes
  4.  
  5. Encode S using the codec registered for encoding. Default encoding
  6. is 'utf-8'. errors may be given to set a different error
  7. handling scheme. Default is 'strict' meaning that encoding errors raise
  8. a UnicodeEncodeError. Other possible values are 'ignore', 'replace' and
  9. 'xmlcharrefreplace' as well as any other name registered with
  10. codecs.register_error that can handle UnicodeEncodeErrors.
  11. """
  12. return b""

样例:

  1. name = '你好'
  2. print(name)
  3. a = name.encode('gbk') #编码转换utf8->uncode->gbk
  4. print (a)
  5.  
  6. 你好 #utf-8编码显示结果
  7. b'\xc4\xe3\xba\xc3' #gbk编码显示结果

encode

9. startswith

描写:判断以什么字符开头,结果范围True or False

语法:

  1. def startswith(self, prefix, start=None, end=None): # real signature unknown; restored from __doc__
  2. """
  3. S.startswith(prefix[, start[, end]]) -> bool
  4.  
  5. Return True if S starts with the specified prefix, False otherwise.
  6. With optional start, test S beginning at that position.
  7. With optional end, stop comparing S at that position.
  8. prefix can also be a tuple of strings to try.
  9. """
  10. return False

样例:

  1. name = 'allen'
  2. a = name.startswith('a') #判断字符串以a开头
  3. print(a)
  4.  
  5. True #显示结果
  6.  
  7. name = 'allen'
  8. a = name.startswith('a',0,4) #判断‘allen’是否以a开头
  9. print(a)
  10.  
  11. True #显示结果

startswith

10. endswith

描写:判断以什么字符结尾,结果范围True or False,需注意取值与startswith略有不同

语法:

  1. def endswith(self, suffix, start=None, end=None): # real signature unknown; restored from __doc__
  2. """
  3. S.endswith(suffix[, start[, end]]) -> bool
  4.  
  5. Return True if S ends with the specified suffix, False otherwise.
  6. With optional start, test S beginning at that position.
  7. With optional end, stop comparing S at that position.
  8. suffix can also be a tuple of strings to try.
  9. """
  10. return False

样例:

  1. name = 'allen'
  2. a = name.endswith('n',1,5) #判断‘allen’是否以n结尾
  3. print(a)
  4.  
  5. True #结果显示

endswith

11. expandtabs

描写:将tab转换成8个空格

语法:

  1. def expandtabs(self, tabsize=8): # real signature unknown; restored from __doc__
  2. """
  3. S.expandtabs(tabsize=8) -> str
  4.  
  5. Return a copy of S where all tab characters are expanded using spaces.
  6. If tabsize is not given, a tab size of 8 characters is assumed.
  7. """
  8. return "

样例:

  1. name = 'a\tllen'
  2. result=name.expandtabs() #将1个tab转换成8个空格
  3. print(result)
  4.  
  5. a llen #显示结果

expandtabs

12. find

描写:查找序列,返回所在的位置下标。若没有找到,结果返回值为-1

语法:

  1. def find(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
  2. """
  3. S.find(sub[, start[, end]]) -> int
  4.  
  5. Return the lowest index in S where substring sub is found,
  6. such that sub is contained within S[start:end]. Optional
  7. arguments start and end are interpreted as in slice notation.
  8.  
  9. Return -1 on failure.
  10. """
  11. return 0

样例:

  1. name = 'allen'
  2. result = name.find('e') #查找字符是否包含e
  3. print(result)
  4.  
  5. 3 #结果显示,找到e并返回所在下标值
  6.  
  7. name = 'allen'
  8. result = name.find('x') #查找字符是否包含x
  9. print(result)
  10.  
  11. -1 #结果显示为找到,返回值为-1

find

13. index

描写:查找序列,返回所在的位置下标。若没有找到,结果为报错

语法:

  1. def index(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
  2. """
  3. S.index(sub[, start[, end]]) -> int
  4.  
  5. Like S.find() but raise ValueError when the substring is not found.
  6. """
  7. return 0

样例:

  1. name = 'allen'
  2. result = name.index('aa') #查找字符中是否包含aa
  3. print(result)
  4.  
  5. NameError: name 'name' is not defined #未找到,结果显示报错信息

index

14. count

描写:搜索关键字并计数

语法:

  1. def count(self, sub, start=None, end=None): # real signature unknown; restored from __doc__
  2. """
  3. S.count(sub[, start[, end]]) -> int
  4.  
  5. Return the number of non-overlapping occurrences of substring sub in
  6. string S[start:end]. Optional arguments start and end are
  7. interpreted as in slice notation.
  8. """
  9. return 0

样例:

  1. name = 'aasd;asdfasdfas'
  2. a = name.count('df') #搜索‘df’的个数
  3. print(a)
  4.  
  5. 2 #显示结果
  6.  
  7. name = 'aasd;asdfasdfas'
  8. a = name.count('df',0,10) #在0-10下标内,所搜‘df’的个数
  9. print(a)
  10.  
  11. 1 #显示结果

count

15. format

描写:字段拼接,类似于%s

语法:

  1. def format(*args, **kwargs): # known special case of str.format
  2. """
  3. S.format(*args, **kwargs) -> str
  4.  
  5. Return a formatted version of S, using substitutions from args and kwargs.
  6. The substitutions are identified by braces ('{' and '}').
  7. """
  8. pass

样例:

  1. name = "aa {name} {id}"
  2. result = name.format(name='allen',id='') #变量赋值,拼接内容
  3. print(result)
  4.  
  5. aa allen 123 #显示结果

format

16. 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 ""

样例:

  1. name = 'allen'
  2. result=name.replace('a','e') #将字符a更改为e
  3. print(result)
  4.  
  5. ellen #显示结果
  6.  
  7. name = 'allen'
  8. result=name.replace('l','o',1) #将字符l替换成o,限第一次匹配
  9. print(result)
  10.  
  11. aolen #显示结果

replace

17. partition

描写:搜索关键字,以关键字为分隔符。若未匹配,则显示为空的字符串

语法:

  1. def partition(self, sep): # real signature unknown; restored from __doc__
  2. """
  3. S.partition(sep) -> (head, sep, tail)
  4.  
  5. Search for the separator sep in S, and return the part before it,
  6. the separator itself, and the part after it. If the separator is not
  7. found, return S and two empty strings.
  8. """
  9. pass

样例:

  1. name = 'allenisboy'
  2. result=name.partition('is') #以is为分隔符
  3. print(result)
  4.  
  5. ('allen', 'is', 'boy') #显示结果

partition

18 join

描写:以定义的字符为分隔符,进行内容拼接

语法:

  1. def join(self, iterable): # real signature unknown; restored from __doc__
  2. """
  3. S.join(iterable) -> str
  4.  
  5. Return a string which is the concatenation of the strings in the
  6. iterable. The separator between elements is S.
  7. """
  8. return ""

样例:

  1. list = ['a','b','c','d']
  2. result="_".join(list) #以下滑线为分隔符,进行拼接
  3. print(result)
  4.  
  5. a_b_c_d #结果显示

join

19. split

描写:以定义的字符为分隔符,进行字符串分割

语法:

  1. def split(self, sep=None, maxsplit=-1): # real signature unknown; restored from __doc__
  2. """
  3. S.split(sep=None, maxsplit=-1) -> list of strings
  4.  
  5. Return a list of the words in S, using sep as the
  6. delimiter string. If maxsplit is given, at most maxsplit
  7. splits are done. If sep is not specified or is None, any
  8. whitespace string is a separator and empty strings are
  9. removed from the result.
  10. """
  11. return []

样例:

  1. name ='''
  2. aaa
  3. bbb
  4. ccc'''
  5.  
  6. result=name.split('\n') #以换行为分隔符,进行分割
  7. print(result)
  8.  
  9. ['', 'aaa', 'bbb', 'ccc'] #结果以列表形式显示

split

20. splitlines

描写:以换行符为分隔符,进行字符串分割

语法:

  1. def splitlines(self, keepends=None): # real signature unknown; restored from __doc__
  2. """
  3. S.splitlines([keepends]) -> list of strings
  4.  
  5. Return a list of the lines in S, breaking at line boundaries.
  6. Line breaks are not included in the resulting list unless keepends
  7. is given and true.
  8. """
  9. return []

样例:

  1. name ='''
  2. aaa
  3. bbb
  4. ccc'''
  5. result=name.splitlines() #默认以换行符进行分割
  6. print(result)
  7.  
  8. ['', 'aaa', 'bbb', 'ccc'] #以列表项是显示结果

splitlines

21. strip

描写:移除字符串头尾指定的字符(默认为空格)。

语法:

  1. def strip(self, chars=None): # real signature unknown; restored from __doc__
  2. """
  3. S.strip([chars]) -> str
  4.  
  5. Return a copy of the string S with leading and trailing
  6. whitespace removed.
  7. If chars is given and not None, remove characters in chars instead.
  8. """
  9. return ""

样例:

  1. name = '123allen'
  2. result=name.strip('') #移除包含1、2的字符
  3. print(result)
  4.  
  5. 3allen #显示结果
  6.  
  7. result=name.strip('') #移除包含1、2的字符
  8. print(result)
  9.  
  10. 3allen #显示结果

strip

21. lstrip

描写:左匹配字符串,进行移除操作

语法:

  1. def lstrip(self, chars=None): # real signature unknown; restored from __doc__
  2. """
  3. S.lstrip([chars]) -> str
  4.  
  5. Return a copy of the string S with leading whitespace removed.
  6. If chars is given and not None, remove characters in chars instead.
  7. """
  8. return ""

样例:

  1. name = '123allen'
  2. result=name.lstrip('') #从左开始,搜索字符1,2,3并移除
  3. print(result)
  4.  
  5. allen #显示结果

lstrip

22. rstrip

描写:右匹配字符串,进行移除操作

语法:

  1. def rstrip(self, chars=None): # real signature unknown; restored from __doc__
  2. """
  3. S.rstrip([chars]) -> str
  4.  
  5. Return a copy of the string S with trailing whitespace removed.
  6. If chars is given and not None, remove characters in chars instead.
  7. """
  8. return ""

样例:

  1. name = '123allen'
  2. result=name.rstrip('en') #从右边开始,搜索en并移除
  3. print(result)
  4.  
  5. 123all #显示结果

rstrip

 

Python之字符串详解1的更多相关文章

  1. python 03 字符串详解

    1.制表符 \t str.expandtabs(20) 可相当于表格 2.def   isalpha(self) 判断是否值包含字母(汉字也为真),不包含数字 3.def   isdecimal(se ...

  2. python之字符串详解2

    逻辑判断字符串类型,返回布尔值 1. islower 描述:判断所有字符是否为小写 语法: def islower(self): # real signature unknown; restored ...

  3. Python变量和字符串详解

    Python变量和字符串详解 几个月前,我开始学习个人形象管理,从发型.妆容.服饰到仪表仪态,都开始做全新改造,在塑造个人风格时,最基础的是先了解自己属于哪种风格,然后找到参考对象去模仿,可以是自己欣 ...

  4. python time模块详解

    python time模块详解 转自:http://blog.csdn.net/kiki113/article/details/4033017 python 的内嵌time模板翻译及说明  一.简介 ...

  5. 【python进阶】详解元类及其应用2

    前言 在上一篇文章[python进阶]详解元类及其应用1中,我们提到了关于元类的一些前置知识,介绍了类对象,动态创建类,使用type创建类,这一节我们将继续接着上文来讲~~~ 5.使⽤type创建带有 ...

  6. Python开发技术详解PDF

    Python开发技术详解(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1F5J9mFfHKgwhkC5KuPd0Pw 提取码:xxy3 复制这段内容后打开百度网盘手 ...

  7. python之数据类型详解

    python之数据类型详解 二.列表list  (可以存储多个值)(列表内数字不需要加引号) sort s1=[','!'] # s1.sort() # print(s1) -->['!', ' ...

  8. (转)python collections模块详解

    python collections模块详解 原文:http://www.cnblogs.com/dahu-daqing/p/7040490.html 1.模块简介 collections包含了一些特 ...

  9. Python之print详解

    Python之print详解 http://www.jb51.net/article/55768.htm   print的一些基本用法,在前面的讲述中也涉及一些,本讲是在复习的基础上,尽量再多点内容. ...

随机推荐

  1. Android 5.0之前屏幕截图的方法

    截图的几种方法 Android获取屏幕截图主要有以下三种方法 1.通过view.getDrawingCache()获取指定View的绘制缓存来实现截屏. 这种方式Android 5.0之前也可以,且不 ...

  2. Spring学习---JPA配置和使用

      理论的东西如果不实践,永远不会变成自己的东西.本文将介绍用maven管理,用Hibernate作为JPA供应商,使用MYSQL数据库,配置和使用JPA.   以下代码已经上传至GITHUB.   ...

  3. 详解Objective-C的meta-class 分类: ios相关 ios技术 2015-03-07 15:41 51人阅读 评论(0) 收藏

    比较简单的一篇英文,重点是讲解meta-class.翻译下,加深理解. 原文标题:What is a meta-class in Objective-C? 原文地址:http://www.cocoaw ...

  4. java的HashCode和equals

    什么时候用到hashcode,什么时候用到equals? 首先java为每个对象都生成有默认的hashcode,这个java core里说是java对象的内存地址,但是equals方法里比较的也是对象 ...

  5. C++ CRTP singleton

    C++ CRTP 是个很有意思的东西,因为解释原理的文章很多,但是讲怎么用的就不是很多了. 今天就稍微写下CRTP(奇异递归模板模式)的一个有趣的用法:Singleton(单例模式) 单例有很多中写法 ...

  6. 2781: [JSOI2007]文本生成器

    2781: [JSOI2007]文本生成器 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 8  Solved: 4[Submit][Status][We ...

  7. mysql配置主从数据库

    1.目的 1.1 实现数据备份 1.2 项目访问时可以实现读写分离,提高访问和操作数据的速度<读写分离好处> 2.背景 这次的主从配置主要实现主库数据的改变可以实现同步到从库中: 此次试验 ...

  8. Objective-C中的Hello World

    Objective-C在C语言的基础之上,加入了自己的一些独特的特性,而且在Objective-C中兼容C语言原有的用法.在Objective-C中,实现Hello World和C语言有一定的区别,如 ...

  9. Spring mvc系列一之 Spring mvc简单配置

    Spring mvc系列一之 Spring mvc简单配置-引用 Spring MVC做为SpringFrameWork的后续产品,Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块 ...

  10. 一点养老APP模式定制系统平台源码

    一点养老APP模式定制系统开发:136.1013.1824电/微:搭建一点养老APP模式定制系统平台.专注于为企业和商家客户提供基于腾讯微信公众平台系统程序和APP等开发服务,中国养老金融50人论坛2 ...