Python novice tutorial 2, strings

2.1 Create variables

name = 'Bob'

To create a string, we only need to wrap the text with a pair of single quotation marks [''] or double quotation marks [""], just like this, you can create a string.

For example:

'182xxxxxxxxxxxx'
"182xxxxxxxxxxxxx"
'Bob'
"Bob"

You can also put the string in a variable, like this:

name = 'Bob'

2.2 splicing strings

In Python, you can also use "+" plus to concatenate two strings into one string. Just like the following:

'今天的天气是' + '晴'

We just spliced ​​['Today's weather is'] and ['clear'] into a string ['Today's weather is sunny'].

2.3 Use a for loop to traverse the string

For data such as strings, we can use a for loop to get every character in the string.

like this:

name = 'Bob'
for i in name:
    print(i)

Running this code, we can find that what is printed in the console is a character: B o b.

2.4 Small test

a = '电话号码是'
b = '6776638'
c = '小明的'
d = 
print(d)

Please complete the code, use the + sign to splice the three variables of abc together in a certain order, and print out the sentence'Xiao Ming's phone number is 676638'.

The correct answer will be announced in the next issue

Answer from the previous issue: C C

Guess you like

Origin blog.csdn.net/m0_52519239/article/details/111413862