Вы находитесь на странице: 1из 1

Microsoft: DEV274x

Introduction to Python: Fundamentals Help zahideme 

Course Discussion Wiki Progress

Course  Module 3 Sequence Iteration  between strings & lists  Cast String to List

 Previous       Next 

Audit Access Expires Apr. 14, 2019


You lose all access to this course, including your progress, on Apr. 14, 2019.
Upgrade by Mar. 31, 2019 to get:
Unlimited access as long as the course exists
Graded problems and exams
Certi cate of completion
Upgrade now

Cast String to List


 Bookmark this page

More Python string tools (tricks?)


Start of transcript. Skip to the end.

MSXIPFXX2017-V003200
Watch later Share

Here are some other useful tricks that we can


use when working with
strings.
For my rst string trick, I'm gonna cast a string
into a list.
So we have the string Hello, and we're just
casting it into a list.
We can see that we get each character
created as a list item by casting to a list.
Above here, I cast to a list and then I rejoin the
list together
 0:00 / 2:32  Speed 1.0x    
with a character here, a tilde.

Video Transcripts
Download video le Download SubRip (.srt) le
Download Text (.txt) le

Concept

More Python string tools (tricks?)

Cast a string to a list of characters


hello_letters = list("Hello")

print to the same line with multiple print statements (end=)


or insert any character as an end in print("String", end="+")

print('Hello', end = '')


print('world')

Examples
# [ ] review and run example
hello_letters = list("Hello")
print(hello_letters)

# [ ] review and run example


# cast sting to list
word_letters = list("concatenates")

# .join() concatenates the list


# print on same line setting the end character
print('~'.join(word_letters))

# [ ] review and run example


print("Hello ", end = '')
print("world")

# [ ] review and run example


# This is the default print end
print("Hello World!", end="\n")
print('still something to learn about print()')

# [ ] review and run example


# end inserts any valid str character: A-z, 0-9,!,@,*,\n,\t or ''(empty string)...
for letter in "Concatenation":
print(letter, end='*')

Task 5
end=" " con guration in printing

print('The String', end='')

# [ ] use 3 print() statements to output text to one line


# [ ] separate the lines by using "- " (dash space)

Task 6
cast: str to list
Msg_characters = list("Always test your code")

# [ ] create a string (fact) of 20 or more characters and cast to a list (fact_letters)


# [ ] iterate fact, printing each char on one line, except for spaces print a new line

Task 7
Program: add the digits
create a 20 digit string, and cast to a list

then add all the digits as integers

print the equation and answer

Hint: use cast to sum the digits, and .join() to create the equation (1+2+3+...)

# [ ] create add the digits

 Previous Next 

Learn About Veri ed Certi cates


© All Rights Reserved

edX Legal Connect


     
About Terms of Service & Blog
edX for Business Honor Code Contact Us
Privacy Policy Help Center
Accessibility Policy

© 2012–2019 edX Inc.


EdX, Open edX, and MicroMasters are
registered trademarks of edX Inc. | 粤ICP备
17044299号-2