Python-Based String explanation and python-Based String explanation
1. Python environment Configuration:
Python Official Website: https://www.python.org/
Pycharm http://www.jetbrains.com/pycharm/download
Install the SDK after the download. Check the environment variables.
2. Note the code indentation when writing python.
2. String:
(1) The string is indexed from left to right from 0 and from right to left from-1. Follow the principle of no package before the package.
Example:
Code:Explanation:Output result:
A = 'abcdefghjk'
Print a outputs a abcdefghijk
Print a [0] outputs 1st elements
Print a [] outputs 3rd to 5th elements cde
Print a [2:] The output string cdefghijk starts from 3rd characters
Print a [-1] outputs 1st k Records from right to left
Print a [-3:-1] starts from the last 3rd (before and after the same package) ij
Print a [-3:] from the last 3rd to the last ijk
Print a [:] output the entire string abcdefghijk
Print a [3:] defghijk starts from 4th characters
Print a [-8:] Output defghijk from the right side of the page
Print a * 2 output abcdefghijkabcdefghijk twice
(2) supported format strings:
Example:
Name = 'bob'
Age = 16
Print ("% s is % d-year-old" % (name, age ))
Output result: Bob is 16-year-old
(3) string Detection
Example:
Code: Explain the output result
S = 'helloabcdword'
Print s. isalpha () to check whether the string is composed of letters. True
Print s. isdigit () to check whether the string is composed of digits. False
Print s. isspace () to check whether the string is composed of spaces. False
Print s. startswith ('hello') to check whether the string starts with 'Hello'. True
Print s. endswith ('World') to check whether the string ends with 'word' and False
(3) uppercase and lowercase letters
Example:
Code: Explanation
A = 'in \ na line' \ n In a string without r will wrap the string
B = r'in \ na line' character strings added with r are not escaped
Print
Print B
Print a. lower () to lowercase
Print B. upper () to uppercase
Output result:
In
A line
In \ na line
In
A line
IN \ NA LINE
(3) string replacement
Example:
Code:
Weather = 'rainy Day'
Bag = 'nothing in the bag'
If weather. find ('rain ')! =-1:
Bag = bag. replace ('noth', 'umbrebala ')
Print bag
Explanation:
If 'rain' is matched, 'Noting' will only be 'umbreler'
The find function does not match the character and the returned value is '-1 '.
Output result:
Umbrella in the bag
Today's sharing is here ~~~~~~~