Home > other >  Beginners tutorial,, every brother please answer.
Beginners tutorial,, every brother please answer.

Time:01-06

Learn a few days, learn list, met this problem, when doing the exercise did not put the fourth row, understand, why fruit [6:] STR type?
(the first one is an error prompt, the second is the result of the added after STR function, the third is exercises)
Problem (text code for:
Fruit=[' orange ', 'watermelon', 'pineapple', 'banana', 'apple', 'mango, grapefruit, pomegranate,' winter date]
Print (' The first three items in The list are: "+ STR (fruit [3] :))
Print (' The items from The middle of The list are: "+ STR (fruit [set]))
Print (' in The last three items in The list are: '+ fruit [6:]))

CodePudding user response:

1, because you print it using the "+" (plus), use plus means string concatenation in here;
2, you see you in the first two printing, casting, were carried out in front of the STR;
3, use the "+" (plus) string concatenation, "+" (plus) type must be consistent, to STR type,
4, for example, you use:
 print (fruit [6:]) 

Can see the results for the
 [' grapefruit ', 'pomegranate', 'winter date] 

This is a list, not a string, so you to be in the front and cast STR:
 print (' The last three items in The list are: "+ STR (fruit) [6:]) 

The results as follows:
 The last three items in The list are: [' grapefruit, pomegranate, 'winter date] 

CodePudding user response:

Add again, if you don't want to casting, you to directly enter the value you need to print, shall not apply to the "+" string concatenation (plus), change "+ (plus)" to ", "(comma), as follows:
 print (' The last three items in The list are: ', fruit [6:]) 

The result is:
 The last three items in The list are: [' grapefruit ', 'pomegranate', 'winter date] 

And the result is the same as you want

CodePudding user response:

NoamaNelson
reference 4 floor response:
add again, if you don't want to casting, you to directly enter the value you need to print, shall not apply to the "+" (plus sign) for string concatenation, changed "+ (plus)" to ", "(comma), as follows:
 print (' The last three items in The list are: ', fruit [6:]) 

The result is:
 The last three items in The list are: [' grapefruit ', 'pomegranate', 'winter date] 

And the results you want is the same as

Thank you for your bosses
  • Related