Python Learning notes-(V) Strings & Dictionaries

Source: Internet
Author: User

1. String Manipulation
>>> name = ("My name is cc") #首字母大写 >>> print (name.capitalize ()) My name is cc>>> print (Name.coun T (' C ')) #统计 the number of ' C ' 2>>>print (Name.center (), "-") #输出50个字符, the name is centered, and both sides are padded--------------------my name is CC------------------->>> Print (Name.endswith ("-")) #判断是否以 "-" End false>>> name = ("My name is\t cc") >>> print (Name.expandtabs ()) #把字符串中的 the tab sign (' \ t ') to a space, the default number of spaces for the tab sign (' \ t ') is 8. My name is cc>>> print (Name.isdigit ()) #判断是否为整数False >>> name = ("my") >>> print (Name.isiden Tifier ()) #判断是不是一个合法的标识符True >>> name = (' 2my ') >>> print (Name.isidentifier ()) false>>> name = ("my") >>> print (Name.islower ()) #是不是小写字符True >>> name = ("my") >>> print (Name.isnumeric ()) #判断是不是纯数字False >>> name = ("my") >>> print (Name.isspace ()) #判断是不是空格False >>> name = ("My name is ") >>> print (Name.istitle ()) #判断是不是title (uppercase letters) true>>> name = (' MY ') >>> print(Name.isupper ()) #判断是否全是大写Truejoin () stitching string, can specify stitching string, such as:+>>> name = (' cc ', ' mm ', ' nn ') >>> print (' + '). Join (name)) cc+mm+nn>>> name = (' cc ') >>> print (Name.ljust ("* ')") #输出50个字符, CC on the far left and right with * cc****** >>> name = (' cc ') >>> print (Name.rjust (' * ')) #输出50个字符 , CC on the far right, left with * ************************************************cc>>> name = (' MY ') >>> print (            Name.lower ()) #小写my >>> name = (' my ') >>> print (Name.upper ()) #大写MY >>> name = (' yy ') >>> print (Name.strip ()) #自动去掉左右的回车和空格yy >>> print (Name.lstrip ()) #自动去掉左边的空格和回车yy >>> p Rint (Name.rstrip ()) #自动去掉右边的空格和回车 the Yymaketrans () method is used to create a conversion table of character mappings, for the simplest invocation of two parameters, the first argument is a string that represents the character that needs to be converted, The second parameter is also the target of the string representation transformation. Note: The length of the two strings must be the same for the one by one corresponding relationship. >>> a= ' hello! world! ' >>> t = A.maketrans (' l ', ' a ') >>> print (A.translate (t)) heaao! worad!>>> name = "CC is UU" >>> prinT (Name.replace ("C", "L", 1)) #替换, 1 represents the replacement of several LC is UU (translate are one by one mappings of characters.) Each character is replaced with the corresponding character as soon as it appears. Replace is a string substitution, and the string is replaced as a whole. The two string arguments for replace can vary in length.) >>> name = ' Ccisuu ' >>> Print (Name.rfind ("U")) #从左往右找到最右边满足条件的下标并返回5 >>> name = (' Cc,nn ') >>> print (Name.split (', ')) #分割字符串, The default is separated by a space [' CC ', ' nn ']>>> name = (' Cc\nuu ') >>> print (Name.splitlines ()) separated by line break [' cc ', ' UU ']>>> name = ' CC ' >>> print (Name.swapcase ()) #大小写转换CC

2. Dictionaries

Python Learning notes-(V) Strings & Dictionaries

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.