博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python3_Str
阅读量:6453 次
发布时间:2019-06-23

本文共 5199 字,大约阅读时间需要 17 分钟。

name = qiuwenbin

name = str('qiuwenbin')----按住ctrl点击方法,通过pycharm能成功定位方法位置

name = str('qiuwenbin')

result = name.__contains__('qu')
result = "qiu" in name

  上面的两种方式一样

print(result)

常用的方法:

  1.首字母大写

def capitalize(self): # real signature unknown; restored from __doc__        """        S.capitalize() -> str                Return a capitalized version of S, i.e. make the first character        have upper case and the rest lower case.        """        return ""
View Code

  2.首字母边小写

def casefold(self): # real signature unknown; restored from __doc__        """        S.casefold() -> str                Return a version of S suitable for caseless comparisons.        """        return ""
View Code

  3.居中显示,并且自定义两边字符串

1 name = str('qiuwenbin')2 result = name.center(20,"*")3 print(result)
View Code

  4.统计数量

name = str('qiuwenbinasdadasdqwqqqweqq qweqwe')

result = name.count("q",0,10)
print(result)

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

  5.判断一个对象是以什么结尾

name = "qiuwenbin"

result = name.endswith("n")
print(result)

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

  6.tab(/t)变成空格

name = "qiuwen\tbin"

result = name.expandtabs()
print(result)
print(len(result))

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

   7.找出字符串所在对象中的位置

name = "qiuwen\tbin"

result = name.find("n")
print(result)

    ---与index的方法类似,但是find如果找不到返回值是“-1”,index没有找到则返回报错

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

  8.返回一个格式化的版本

name = "qiu {0} wen{1} bin{2}"

result = name.format('wang','xiao','qin')
print(result)

def format(self, *args, **kwargs): # known special case of str.format        """        S.format(*args, **kwargs) -> str                Return a formatted version of S, using substitutions from args and kwargs.        The substitutions are identified by braces ('{' and '}').        """        pass
View Code

  9.列表拼接

lsit = ['s','b','x','x','L']

result = "".join(lsit)
print(result)

&

lsit = ['s','b','x','x','L']

result = str("$").join(lsit)
print(result)

def join(self, iterable): # real signature unknown; restored from __doc__        """        S.join(iterable) -> str                Return a string which is the concatenation of the strings in the        iterable.  The separator between elements is S.        """        return ""
View Code

  10.分隔符,保留分割关键字。

name = "qiuwenbinlovewangxiaoqin"

result = name.partition("love")
print(result)

def partition(self, sep): # real signature unknown; restored from __doc__        """        S.partition(sep) -> (head, sep, tail)                Search for the separator sep in S, and return the part before it,        the separator itself, and the part after it.  If the separator is not        found, return S and two empty strings.        """        pass
View Code

  11.替换

name = "qiuwenbinlovewangxiaoqin"

result = name.replace("a","Y",2)
print(result)

def replace(self, old, new, count=None): # real signature unknown; restored from __doc__        """        S.replace(old, new[, count]) -> str                Return a copy of S with all occurrences of substring        old replaced by new.  If the optional argument count is        given, only the first count occurrences are replaced.        """        return ""
View Code

  12.splitlines()方法

name= '''11

22
33'''
result = name.splitlines()
print(result)

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

  13.换行符处理,\n是用于str内部的

>>> print(b'abc\ndef')b'abc\ndef'>>> print('abc\ndef')abcdef>>> type(b'')
>>> type('')

 

转载于:https://www.cnblogs.com/Bingo0-python/p/6837537.html

你可能感兴趣的文章
xargs 命令
查看>>
我的友情链接
查看>>
CHTools-OC版本目录介绍
查看>>
Rsync详解
查看>>
在JavaScript中创建对象
查看>>
SpringCloud学习成长之路二 服务客户端(rest+ribbon)
查看>>
HTTP 与 Post
查看>>
[转载]真正的inotify+rsync实时同步 彻底告别同步慢
查看>>
DAX/PowerBI系列 - 关于时间系列 - 时间相关数值比较 - 用非自带函数
查看>>
BestCoder Round #72 (div.2)
查看>>
时间处理工具类TimeUtil
查看>>
3.《Spring学习笔记-MVC》系列文章,讲解返回json数据的文章共有3篇,分别为:...
查看>>
struts2.1.6 + hibernate3.3 + spring3.0 遇到的问题
查看>>
mongoDB authentication
查看>>
db2常见异常
查看>>
JAVA基础--线程
查看>>
dom操作节点-----怎样添加、移除、移动、复制、创建和查找节点?
查看>>
使用 PowerDesigner 和 PDMReader 逆向生成 MySQL 数据字典
查看>>
详细解释:nginx中ngx_http_access_module模块(HTTP Access 模块)配置及各个参数含义
查看>>
3.2 采购管理目标
查看>>