String (first article)

Create a string

1. Create a single string or double quotes, for example: name = 'elephant', profession = "test"

2. three consecutive single quotes alive three double quotes can help us create a multi-line strings, for example,

= stringOne '' ' 
I was a test, the test on the road I would have been ahead, and constantly strive to learn, 
believe in yourself, love your favorite. . . . 
'' ' 
StringTwo = "" " 
I was a test, the test on the road I would have been ahead, and constantly strive to learn, 
believe in yourself, love your favorite !!!! 
" "" 
Print (stringOne)
 Print ( stringTwo)

2. Empty string does not contain the characters and a length of 0 Rehe such as: c = ''

len () function

 

1 stringOne = '' ' 
2  I am a test, the test on the road I would have been ahead, and constantly strive to learn,
 3  believe in yourself, love your favorite. . . .
4  '' ' 
5 stringTwo = "" " 
6  I was a test, the test on the road I would have been ahead, and constantly strive to learn,
 7  believe in yourself, love your favorite !!!!
 8  " "" 
9 = C '' 
10  Print (len (stringOne))
 . 11  Print (len (C))
View Code

operation result:

49
0

Escape character

Escape character

description

\ (When the end of the line)

Continuation character

\\

Backslash

\'

apostrophe

\"

Double quotes

\b

Backspace (Backspace)

\n

Wrap

\t

Horizontal tab

\r

Enter

 

String concatenation

+ 1 can be spliced ​​together multiple strings. For example: 'aa' + 'bb' ==> 'aabb'.

 (1) If + sides are strings, the splice.
 (2) + if both sides are digital, the addition operator. 

 (3) + if different types on both sides, an exception is thrown.

2 may be implemented directly into a plurality of literal strings spliced ​​together. For example: 'aa''bb' ==> 'aabb' 

String was not in a new line

Call print when the parameter end = "arbitrary string" to achieve without a new line

1 print("ssss",end="-")
2 print("xxxx",end="")
3 print("tttt")

Operating results: ssss-xxxxtttt

Reads a string from the console


input()

str () digital transition string 

str () function can be converted to other types of data strings, such as: str (1.2) -> '1.2' str (True) -> 'True'

Of the strings

1. subscripts extracted

 Is essentially a sequence of characters of the string, we can append a string [], specifies the offset in [which] can extract a single character at that position. Forward search:

 A first left-most character, the offset is 0, the second shift amount is 1, and so on. Until len (str) -1 up.

 Reverse Search: rightmost character first, offset is -1, the penultimate offset is -2, and so on, until -len (str).

1 stringOne = '' ' I was a test, the test on the road I would have been ahead, and constantly strive to learn,
 2  believe in yourself, love your favorite. . . . '' ' 
. 3  Print (len (stringOne))
 . 4  Print (stringOne [0])
 . 5  Print (stringOne [. 3 ])
 . 6  Print (stringOne [-1 ])
 . 7  Print (stringOne [47-1])

operation result:

47
my
name
.
.

2. Using the extracted slice slices

 Slice slice operation allows us to quickly extract a substring. Standard format is: [start offset start: Ending offset end: step step] 

Operating instructions and

Examples

result

[:] Extraction entire string

“abcdef”[:]

“abcdef”

[Start:] start from the beginning to the end of the index

“abcdef”[2:]

“cdef”

[: End] from the beginning know end-1

“abcdef”[:2]

"from"

[Start: end] from start to end-1

“abcdef”[2:4]

“cd”

[Start: end: step] extracted from start to end-1, step a step

“abcdef”[1:5:2]

“bd”

 

Guess you like

Origin www.cnblogs.com/elephant-study/p/11535862.html