Python string manipulation

Source: Internet
Author: User

String output:
%s print string;%d print digit;%f print decimal;%u print positive number
Example: Print ("Name:%s"%name)
%2d occupies two bytes, by default right-aligned
%-2d occupies two bytes, left justified
String input:
UserName = input ("Please enter user name:")
Print ("User name:%s"%username)
Python2 input string with raw_input ()
Slice:
Name = "Xiaoming"
Name[0:3] from index 0 to index 3, containing 0 does not contain 3
Name[0:3:1] The last one represents the stride size.
NAME[-1] The first one from the right.
Name[3:] start at 3 and cut to the end.
Name[-1:-3:-1] Starts from the right, and the step size is negative, and the string order is reversed.
NAME[::-1] Reverses the entire string order.
String manipulation:
1. Find
Mystr.find (Str,start=0,end=len (MYSTR))
Detects if STR is contained in MYSTR, or returns 1 if it is the index value that returns the start.
2. Index
Just like the Find () method, only if STR does not report an exception in MyStr.
Rfind,rindex are all from the right to start looking.
3. Count
Mystr.count (Str,start=0,end=len (MYSTR))
Returns the number of times that STR appears in mystr between start and end.
4. Replace
Mystr.replace (Str1,str2,mystr.count (STR1))
Replace the str1 in mystr with STR2, and if Count is specified, the number of replacements is no more than count.
5. Split
Mystr.split (str= "", 2)
Slice mystr with the Str delimiter, and the value behind indicates a few cuts from the left.
6, Capitalize
Mystr.capitalize ()
Capitalize the first character of a string
7. Title
Capitalize the first letter of each word in a string
8, StartsWith
Mystr.startswith (obj)
Checks whether the string starts with obj, or returns true, otherwise false
9, lower
Mystr.lower ()
Convert all uppercase characters in mystr to lowercase
10, Upper
Mystr.upper ()
Converts lowercase letters in mystr to uppercase.
11, EndsWith
Mystr.endswith (obj)
Checks whether the string ends with obj, or returns False if True.
12, Ljust
Mystr.ljust (width)
Returns the left alignment of an original string and fills the new string with the width of length with a space
13, Rjust
Mystr.rjust (width)
Returns an original string that is right-aligned and fills the new string with the length with a space.
14. Center
Mystr.center (width)
Returns the center of the original string and fills the new string with a space of length width.
15, Lstrip
Mystr.lstrip ()
Remove white space characters to the left of MyStr
16, Rstrip
Mystr.rstrip ()
Remove whitespace characters at the end of a mystr string
17, strip
Mystr.strip ()
Remove whitespace characters at both ends of a mystr string

18, partition
Mystr.partition (str)
The mystr is divided into three parts, str, str and STR
19, Rpartition
Mystr.rpartition (str)
Similar to the partition () function, but starting from the right.
20, Splitlines
Mystr.splitlines ()
Returns a list that contains rows as elements, separated by rows (Kill \ n in the string)
21, Isalpha
Mystr.isalpha ()
Returns true if all characters in the mystr are letters, otherwise false.
22, IsDigit
Mystr.isdigit ()
Returns True if the mystr contains only numbers, otherwise false is returned.
23, Isalnum
Mystr.isalnum
Returns true if all characters in the mystr are letters or numbers, otherwise false.
24, Isspace
Mystr.isspace ()
Returns True if the mystr contains only spaces, otherwise false
25. Join
Mystr.join (str)
Insert Str after each character in the mystr to construct a new string

Python string manipulation

Related Article

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.