Python (1) string usage and python string usage

Source: Internet
Author: User

Python (1) string usage and python string usage

I. program output: print is in Python2.X. The usage of print is: print "hello word! "In Python3.X, print appears as a function. Usage: print (" hello word !")
Output number: print (1) Output string: print ("1") output variable: print (name)
Example: print a sentence: hello, word!
Print ("hello, word! ")
2. program input: input
In the conventional programming process, you can directly use input or assign the input method to an object.
Example: name = input ("enter your name:") input ("enter your name :")
Start-up programming: The program prompts users to enter their names, ages, and hobbies in turn, and displays user input to users.
Iii. break and continue
1. break: recyclically 10 times. If you encounter break for 3rd Times, the subsequent 4-10 cycles will not be executed and will jump out. 2. continue: 10 times in a loop, 3rd times in a continue, 3rd times in a loop, and 4-10 times in a later stage.
Iv. Slicing
1, name [0: 5]: Cut the first to fourth character 2, name [0:] Cut the first to the last character 3, name [-1] Cut the last character 4, when name [] is used to cut the first to fourth characters, it starts from the first character and jumps one character to one. 5, name [-1:-1] is switched from the last flashback to the first, which can be omitted as: name [:-1]
5. Common Operations on strings
1,Query subscript find, index
: Find/index: find whether the string exists in a variable and return the subscript of the variable from left to right;

1.1. If the find element is not found,-1 is returned.
If no element is found in the index, a value exception is returned.



1.2. If the subscript is obtained from right to left, rfind/rindex is used.
2,Query count
1. count queries the number returned by an element in a string. 2. If the return value is 0, no number is returned;
3. replace the output replace
1, name. replace ("original string", "replaced string") 2. Note: All string tuples belong to an unchangeable type. Here the replacement only changes when the output is changed;
4. Determine the first and last fields: startwith, endwith
1, name. startwith ("") judge the first field of the string 2, name. endwith ("") judge the end field of the string
5. The converted string is case-sensitive, left-right-aligned, and centered.
1. lower () converts all elements in the string to lowercase 2, upper () converts all elements in the string to uppercase 3, and title () converts all elements in the string to uppercase
4, name. center (50) center alignment 5, name. ljust (50) Align to left 6, name. Align ust (50) Align to right
6. DeleteBlank

1. strip () deletes all white spaces. 2. lstrip () Deletes white spaces starting with 3. rstrip () Deletes white spaces at the end.
7. split (): CutFor example, if you enter several consecutive fields, you can set a special symbol to cut these fields into lists for reading and storing. For example, enter your ID. If multiple IDs are separated by commas (,): 157,258,558, you can use split (",") to cut user input into a list.
 8,Partition Take the string in partition as the center and cut the string into three parts.
9. Determine the string content 9.1,Isalpha ()Determines whether a string contains letters (pure letters, no numbers, spaces, or special characters). If yes, True is returned. If not, False is returned.

 9.2,Isdigit ()Determines whether the string is a pure number. The usage is the same as above 9.3,Isalnum ()Determines whether the string contains digits and letters. The usage is the same as the preceding
9.4,Isspace ()Determines whether the string contains only spaces. The usage is the same as the above 10. join () inserts a new value and constructs a new string.


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.