Python string str method using the

Source: Internet
Author: User

#!usr/bin/env python
#-*-coding:utf-8-*-
#字符串通常用双引号或单引号来表示: ' 123 ', ' abc ', ' String '
#str字符串的常用方法有以下: String can be used to assign a temporary variable s
#友情提示以下全是python2. x version demo, python3.x please print (put in test printing), for example: print (Len (s) )
From string Import Maketrans
s3 = ' 123 '
s2 = ' '
S1 = ' This is \ t Cash '
s= ' abcdefghijklmnopqrstuvwxyz '
S4 = "0000000this is string EXAMPLE....WOW!!! 0000000 "
s5 = ' ab C\n\nde fg\rkl\r\n '
print S[0:6] #切片, intercepts a paragraph in a string (the starting position of the following is the end position)
print Len (s) #统计字符串的长度
print s.ljust (0 '), '--->ljust method ' #返回一个原字符串右用字符串0填充左对齐并宽度50的新字符串
print s.rjust (0 '), '--->rjust method ' #返回一个原字符串左用字符串0填充右对齐并宽度50的新字符串
print s.capitalize (), '--->capitalize method ' #返回字符串首字母大写的副本
Print S.center (' * '), '--->center method ' #表示原字符串居中两边填充宽度的用法
print format (s, '. 3 '), '--->format method ' #格式化输出也可当切片用的方式
print S.upper (), '--->upper method ' #将小写字母转换成大写字母
print S.lower (), '--->lower method ' #将大写字母转换成小写字母
print s3.isdigit (), '--->isdigit method ' #判断字符串如果是数字返回真True, not return false false
print S.startswith (' abc '), '--->startswith method ' #判断字符串是否是以abc开头如果是返回真, otherwise return false
print S.endswith (' zz '), '--->endswith method ' #判断字符串是否以yz结尾是返回真, otherwise return false
print s1.expandtabs (), '--->expandtabs method ' #把字符串中的 tab sign (' \ t ') to a space, the tab symbol (' \ t ') The default number of spaces is 8
print S.isalnum (), '--->isalnum method ' #判断字符串至少有一个字符并且所有字符都是字母或数字则返回 True, otherwise returns False
print S.isalpha (), '--->isalpha method ' #判断字符串至少有一个字符并且所有字符都是字母则返回 True, otherwise returns False
print s2.isspace (), '--->isspace method ' #判断字符串中只包含空格, returns True, otherwise False
print S1.istitle (), '--->istitle method ' #如果字符串中所有的单词拼写首字母是否为大写, and return True if other letters are lowercase, otherwise False
print S4.strip ("0"), '--->strip method ' #返回移除字符串头尾指定的字符生成的新字符串
print s.find (' SD '), '--->find method ' #检查是否包含在指定范围内, returns 1 if the containing substring returns the starting index value.
print S.index (' s '), '--->index method ' #检查是否包含在指定范围内, just like the Find () method, except if an exception is not reported in the string
print s.partition ("K"), '--->partition method ' #生成一个分隔符k, the first is a substring to the left of the delimiter, the second is the delimiter itself, and the third is the substring to the right of the delimiter.
print s1.swapcase (), '--->swapcase method ' #返回大小写字母转换后生成的新字符串
print s5.splitlines (), '--->splitlines method ' #返回一个包含各行作为元素的列表
print S2.join (S3), '--->join method ' #返回通过指定字符连接序列中元素后生成的新字符串
Print s3.translate (Maketrans (S3,S2)) #返回翻译后的字符串 (a little complicated) confident Baidu Brain
print S3.zfill, '--->zfill method ' #返回指定宽度的字符串原字符串右对齐, front padding 0
#以下打印输出:

Python string str method using the

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.