Python programming quickly get started with tedious work-6.6 exercises

Source: Internet
Author: User

Python programming quickly get started with tedious work-6.6 Exercise 1, what is an escape character?

A: The escape character represents some characters in a string that are otherwise difficult to print in the code.

2. What does the escape character \ n and \ t represent?

Answer: \ n is a newline character, \ t is a tab.

3, how to put a slash character in the string \?

A: the \ Transfer character represents a backslash.

4, the string "howl ' s Moving Castle" is a valid string. Why is the single quotation mark in the word not escaped, but no problem?

A: Howl's single quotation marks are fine because you use double quotation marks to identify the beginning and end of a string.

5. If you don't want to add \ n to the string, how do I write a string with a newline?

A: A multiline string lets you use a newline character in a string without having to use \ n escape characters.

6. What is the expression evaluated below?


' Hello world! ' [1]
' Hello world! ' [0:5]
' Hello world! ' [: 5]
' Hello world! ' [3:]

Answer: ' Hello world! ' [1] = e   'Hello world! ' [0:5] = Hello'Hello world! ' [: 5] = Hello'Hello world! ' [3:] = Lo world!

7. What is the expression evaluated below?

' Hello '. Upper ()
' Hello '. Upper (). Isupper ()
' Hello '. Upper (). Lower ()
For:

' Hello '. Upper () = Hello'hello'. Upper (). Isupper () = True' Hello '. Upper (). lower () = Hello

8. What is the expression evaluated below?

' Remember, Remember, the fifth of Novermber '. Split ()
' _ '. Join (' There can be is only one. '). Split ()

Answer:'Remember, Remember, the fifth of Novermber'. Split () = ['Remember,','Remember,',' the','Fifth',' of','Novermber']'_'. Join ('There can is only one.'). Split ()'_'. Join ('There can is only one.'). Split () = ['t_h_e_r_e_','_c_a_n_','_b_e_','_o_n_l_y_','_o_n_e_.']

9. What string method can be used for right-aligned, left-aligned, and centered strings?

For:
Rjust () is right-justified
Ljust () is left justified
Center () is centered


10. How do I get rid of whitespace characters at the beginning or end of a string?

For:
Strip () is a blank character that is removed from both ends


6.7. Practical Project-form printing

Write a function named PrintTable () that accepts a list of strings and displays it in a well-organized table, with each column
Align to the right. Assume that all inner-level lists contain the same number of strings. For example, the value might look like this:
Tabledata = [[' Apples ', ' oranges ', ' cherries ', ' banana '],
[' Alice ', ' Bob ', ' Carol ', ' David '],
[' Dogs ', ' cats ', ' Moose ', ' goose ']

Your printtable () function will print out:
Apples Alice Dogs
Oranges Bob Cats
Cherries Carol Moose
Banana David Goose

Idea: Use Colwidths as a list to save the width of the longest string in a given list, find the maximum value in the Colwidths list,
Passed to the Rjust () string method.

#!/usr/bin/env Python3#-*-coding:utf-8-*-#Author:davie"""6.7. Practice Item-Form print a function named PrintTable () that accepts a list of strings, displays it in a well-organized table, and aligns each column to the right. Assume that all inner-level lists contain the same number of strings.            For example, the value might look like this: Tabledata = [[' Apples ', ' oranges ', ' cherries ', ' banana '], [' Alice ', ' Bob ', ' Carol ', ' David '], [' Dogs ', ' cats ', ' Moose ', ' goose '] Your printtable () function will print out: Apples Alice dogs oranges Bob catscherries Carol moose Banana David Goose"""Tabledata= [['Apples','oranges','Cherries','Banana'],            ['Alice','Bob','Carol','David'],            ['Dogs','Cats','Moose','Goose']]defprinttable (tabledata): Len_list=[0,0,0] forIndex, iteminchEnumerate (tabledata): forStrinchItem:ifLen (str) >Len_list[index]: Len_list[index]=len (str)#print (len_list)         forSeqinchRange (len (tabledata)):Print(Tabledata[0][seq].rjust (len_list[0]), tabledata[1][seq].rjust (len_list[1]), tabledata[2][seq].rjust (len_list[2])) printtable (tabledata)

Python programming quickly get started with tedious work-6.6 exercises

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.