1.str的函数
String methods
lower()小写
upper()大写
str()将目标转化为str输出
2.print("a " + "b " + " c") = a b c
3.
name = raw_input("What is your name?")
quest = raw_input("What is your quest?")
color = raw_input("What is your favorite color?")
print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, quest, color)
用%s来获得%中的对象,每句之后都要跟着%(),按顺序来。
4.获取时间
from datetime import datetime #导入包
now = datetime.now()
5.
not
is evaluated first;and
is evaluated next;or
is evaluated last.
中的IF需要加冒号的,如if(a>b) :
7.
name = raw_input("What is your name?")
用来打出What is your name?并且接受输入赋给name
8.用from xx import *来导入xx中的全部variable和function
9.my_list = [1,9,3,8,5,7]
for number in my_list:#这种用法第一次见
10.
Write a function called censor
that takes two strings, text
and word
, as input. It should return the text
with the word
you chose replaced with asterisks.
For example:
censor("this hack is wack hack", "hack")
should return
"this **** is wack ****"
- Assume your input strings won't contain punctuation or upper case letters.
- The number of asterisks you put should correspond to the number of letters in the censored word.
def censor(text,word):
new = text.split()
for num in range(len(new)):
if(new[num] == word):
new[num] = "*" * len(word)
return " ".join(new)
说明:
split()函数,string的split()函数,将sting中通过()中的划分,如(how . are .you).split(.) = ["how","are","you"]
join函数,按照分割符连接起来,如" ".join(new)就是将new这个list的元素中通过" "连接起来