Parisgabriel:python Endless Day06

Source: Internet
Author: User

Parisgabriel

basics of getting started with Python

String: str
Used to record text information
How the string is represented:
In non-annotations, all quoted parts are strings.
' Single quotation mark
"" Double quotation marks
"" "Three single primers
"" "" "" three double cited
The content represents a non-empty string otherwise it is an empty string

Difference:
Double quotation marks in single quotation marks do not end with a terminator
Single quotation marks in double quotation marks do not count as Terminator
Line breaks in three quotes are automatically converted to newline characters
quotation marks and double quotation marks can be enclosed in three quotes

Implicit string concatenation:

s = "I ' m a I ' m a programmer" ' Heoll "wored" '

Transfer character:
\ ' Single quotation mark
\ "Double quotation marks
\ \ SLR Slash
\ nthe line break
\ r returns cursor to beginning of line
\ t Horizontal tab
\v Vertical Tab
\f Page Change
\b Backwards
0 null character with a character value of
\xxx, XX is a two-bit, 16-binary representation of the self-character
\UXXXX, XXXX is 4 16 binary Unicode16 characters
\uxxxxxxxx, the Unicode32 character of 8 16 binary representations

ASCII encoding:
Man ASCII : View Help document Commands


Commonly used ASCII encoding:
Character decimal hexadecimal
0 0x30
A-0x41
A-0x61

Unicode:
Unified coding, divided into Unicode16, Unicode32

Concept of sequence:
A string is a sequence
Function:
len (x) returns the number of characters in a string
Raw string:
Make escape symbol \ Invalid
Format:
R ' string content '
Print (R "\n\\\t")
The operation of the string:
+ For stitching strings
* Used to generate duplicate strings
A string can be multiplied only with an integer
+ = *= Same as other conforming operators
In/not in operator:
Used to determine whether a string is in the
Equivalent to a string that has occurred if it has ever returned true otherwise returns false
Comparison operation of strings:
> >= < <= = = =!
Compare by encoding until the shortest string ends

Index (subscript):
The Python string is not able to change the sequence of characters

Index
string [integer-expression]
A python sequence can use an index to access an object in a sequence.
The sequence forward index of Python is the last index that starts at 0, Len (s)-1
The response index of the Python sequence is starting from 1-1 is the last one the first is-len (s)

Slice:
Slice
Extracts the corresponding elements from a sequence of strings to form a string series
Grammar:
String sequence [(start index B): (End index E): (step s)]
The bracketed part represents the omission of the
Description
The start index is where the slice begins to cut, 0 represents the first element, 1 for the second, and 1 for the last
End index is the end index of the slice (but does not include the end point)
Step:
is the direction and offset at which the slice moves after each fetch of the current element
There is no step, which is equivalent to moving the index to the right after the value is completed default 1
When the step is represented as positive, the forward slice step is defaulted to 1 and the start position is 0 and the Len (s) step represents the value growth value of the next index.
Reverse Slice when the step is negative the default starting position is the last element, and the terminating position is the previous position of the first element

Python3 are commonly used in sequence functions:
len (x) returns the sequence length

Max (x) returns the sequence maximum element

min (x) returns the smallest element of a sequence

String Encoding conversion function:
Ord (c) returns the Unicode value of a character (decimal)

chr (i) returns the character corresponding to the value of the integer i

Integer to String function:
bin (i) converts an integer to a binary string

Oct (i) converts an integer to an octal string

Hex (i) converts an integer to a 16-binary string

Binary conversion Other systems are very good.

100=4 011=5 10=2 156=0o452 (octal)

1100=c 1001=9 156=0x9c (hex)

Constructor for string:
Str (obj= "") converts an object to a string

Common String methods:
Documentation See:
Object. Method name (method pass parameter)
"ABC". ISALPHA () # syntax is right
123.isalpha () # syntax is wrong
Method description
s.isdigit () determines whether the characters in a string are all numbers
S.isalpha () determine if the string is all English letters
S.islower () determines whether all characters in a string are all lowercase English letters
S.isupper () to determine whether all characters in a string are all uppercase English letters
s.isspace () determines whether the string is all white-space characters
S.center (Width[,fill]) Center The original string, left and right default padding space width: The length of the center string fill: Default padding space

S.count (sub[, Start[,end]]) gets the number of substrings in a string sub: the string you want to get start: Start position end: End Position
S.find (sub[, Start[,end]]) gets the index of a substring sub in the string, fails back 1 start: Start position end: End Position

S.strip () Returns a string that removes left and right whitespace characters
S.lstrip () Returns a string that removes the left white space character
S.rstrip () Returns a string that removes the right white space character
S.upper () generates a string that converts English to uppercase
s.lower () generates a string that converts English to lowercase
s.replace (old, new[, Count]) replaces the original string with new, generating a new string count: Number of replacements
S.startswith (prefix[, start[, end]) returns False if S is the beginning of the prefix, if True is returned with prefix,
S.endswith (suffix[, start[, end]) returns False if S is the end of suffix and returns true at suffix end

The following are not commonly used
S.title () generates an uppercase string for each English word
s.isnumeric () determines whether a string is a full numeric character

Practice:
1.
Enter a string to bind with S
Judging the input has a few people space
Remove the left and right white space characters from the original string
Print the remaining number of characters
Determine if the input is a number
If it's a number, determine if the number is greater than 100.

Answer:

Print ("Answer to question 1:", "\ n") s = input ("Plaese input at would string:") print ("You input string blank has", s.count (" ")) Print ("String surplus:", Len (S.strip ())) if S.isdigit () is True:    if int (s) >:        print ("Input integer:", S, "& Gt;100 ")    else:        print (" Input integer: ", S," <100 ") Else:    print (" You input contain alpha: ", s)

2.
Enter 3 lines of text so that the three lines of text are centered within a box
Shown below:
+---------------+
| Hello |
| 123123 |
| |
+---------------+
Note: Please do not enter Chinese

Answer:

Print ("Answer to question 2:", "\ n") A = input ("Plaese input at would string 1:") b = input ("Plaese input at would string 2:") c = input ("Plaese input at would string 3:") top = AIF Top < b:    top = bif Top < c:    top = cprint ("+" + "-" * (l En (top) * 2 +) + "+") Print ("|" + A.center (len (top) * 2 + 10) + "|") Print ("|" + B.center (len (top) * 2 + 10) + "|") Print ("|" + C.center (len (top) * 2 + 10) + "|") Print ("+" + "-" * (Len (top) * 2 + 10) + "+")

Parisgabriel:python Endless Day06

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.