字符串方法总结

总结复习字符串的方法,以前用help(str)查看帮助的时候,弹出英文说明,总是看不懂,也不想看,现在结合老师讲的几个方法,以及书上用的方法,把帮助里的英文用汉语翻译了下,帮助新手们理解.

S.center(width[, fillchar]) -> str | (宽度[,填充字符])-> 返回字符串 | Return S centered in a string of length width. 把s放在宽度为(width)的字符串中间 Padding is done using the specified fill character (default is a space) 用指定的填充符填充(默认是一个空格) 举例 'abcdfe'.center() -> ' abcdfe '

count(...) | S.count(sub[, start[, end]]) -> int | 子串,开始,结束 返回整数值 | Return the number of non-overlapping occurrences of substring 返回子串sub在s中非重叠出现的次数 sub in string S[start:end].
Optional arguments start and end are interpreted as in slice notation. 可选变量'开始'和'结束'切片记法的方式被解释 举例: 'abcdfe'.count('e',2) -> 1 endswith(...) | S.endswith(suffix[, start[, end]]) -> bool | 下标 -> 布尔型 | Return True if S ends with the specified suffix, False otherwise. 如果s是以指定的后缀结束则返回True,否则返回False | 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. 如果有可选参数start,则在该位置开始测试 如果有可选参数end,则在该位置结束比较 startswith(...) | S.startswith(prefix[, start[, end]]) -> bool |    前缀         返回布尔型 | Return True if S starts with the specified prefix, False otherwise. 如果s是以指定的前缀开始则返回True,否则返回False | With optional start, test S beginning at that position. | With optional end, stop comparing S at that position. | prefix can also be a tuple of strings to try. |

find(...) | 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]. 当子串sub在s中被找到时返回它的最小的下标值.这个子串必须包含在 s中 Optional arguments start and end are interpreted as in slice notation. | Return -1 on failure. 如果失败返回-1 index(...) | S.index(sub[, start[, end]]) -> int | 索引  子串       返回 整型 | Like S.find() but raise ValueError when the substring is not found. 想S.find()一样,但是当子串没有找到时,raise(使..进入视线)valueerror

upper(...) | S.upper() -> str | 较高的 返回 字符串 | Return a copy of S converted to uppercase. |  返回经过转化成大写的s的复制品 lower(...) | S.lower() -> str | 较小的 返回 字符串 | Return a copy of the string S converted to lowercase. |  返回转化为小写的s的复制品 format(...) | S.format(*args, **kwargs) -> str |
| Return a formatted version of S, using substitutions from args and kwargs. 用参数和关键字参数替换s,并返回这个格式化后的版本 | The substitutions are identified by braces ('{' and '}'). 这个代替物以大括号作为区别标志

isalnum(...) | S.isalnum() -> bool | 都是数字? 返回 布尔型 | Return True if all characters in S are alphanumeric 如果s中都是字母和数字时,返回真.否则返回假 | and there is at least one character in S, False otherwise. | 并且s中必须有一个元素. isalpha(...) | S.isalpha() -> bool |
| Return True if all characters in S are alphabetic 如果s中都是字母返回真,否则返回假 | and there is at least one character in S, False otherwise. s中必须有一个元素 |
isdecimal(...) | S.isdecimal() -> bool | 十进制 | Return True if there are only decimal characters in S, | False otherwise. | 如果在s中只有十进制的元素则返回真,否则返回假 isdigit(...) | S.isdigit() -> bool |
| Return True if all characters in S are digits | and there is at least one character in S, False otherwise. 如果s中都是数字则返回真,否则返回假.s中必须有一个元素 isnumeric(...) | S.isnumeric() -> bool |
| Return True if there are only numeric characters in S, | False otherwise. 如果s中都是数字则返回真 isspace(...) | S.isspace() -> bool |
| Return True if all characters in S are whitespace | and there is at least one character in S, False otherwise. 如果都是空白格返回真 ' ' -> True ' 3 ' -> False istitle(...) | S.istitle() -> bool |
| Return True if S is a titlecased string and there is at least one | character in S, 如果首字母大写,返回真. i.e. upper- and titlecase characters may only | follow uncased characters and lowercase characters only cased ones. | Return False otherwise. 后面只能跟小写字母和非字母类型的字符 'Asdjg38哈大秘密个哦sdmg' -> True 'AdjgsldDG' -> False isupper(...) | S.isupper() -> bool |
| Return True if all cased characters in S are uppercase and there is | at least one cased character in S, False otherwise. 如果s中的字母元素都是大写返回真,否则返回假.s中至少有一个字母元素. 'SDG345DG' -> True 'DGSDsd' -> False islower(...) | S.islower() -> bool |
| Return True if all cased characters in S are lowercase and there is | at least one cased character in S, False otherwise. | 如果字母元素都是小写则返回真.

alphanumerci/alphabetic的区别是前者指字母和数字,后者仅仅指字母.

有个新得就是方法中的is判断类的方法要注意两点:

1,判断的元素是什么?all?only numeric?或者只针对字母元素(cased character)其他元素不考虑.

2,判断点是什么?字母?数字?还是字母和数字?或者大写?小写?首字母大写?等等

其中有个疑问,digit/numeric/decimal这几个单词的区别是什么?用'123.2'和'-123'做测试返回都为False这里输入代码

转载于:https://my.oschina.net/u/3829432/blog/1790586

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值