python基础1--原始字符串加反斜杠 字符串中引号 while循环限制次数

上一篇 / 下一篇  2018-05-16 11:49:18 / 个人分类:python学习

1.原始字符串末尾加反斜杠

常用于字符串中有多个反斜杠“\”的情况,在字符串前加小写字母r即可。

 name = r'吴彦祖\n成龙\n刘德华',print(name),则输出

吴彦祖\n成龙\n刘德华
  • 1

原始字符串最后不能加反斜杠”\”

如果非要加反斜杠,可以在原始字符串后面再用引号括住两个反斜杠,如

name = r'吴彦祖\n成龙\n刘德华''\\'print(name)
  • 1
  • 2

则输出吴彦祖\n成龙\n刘德华 \

2.Python中,在字符串里嵌入引号

1、在双引号前面加个转义符 \ ,即反斜杠。如"Hello  \"W \"orld",会输出  Hello "W"orld

2、用单引号引起整个字符串,如'Hello "W"orld',同样输出  Hello "W"orld

也可以在字符串里嵌入单引号,如"Hello 'W'orld",输出 Hello 'W'orld

3.whlie循环限制次数--加个变量统计下次数,然后while条件语句里加个条件判断

import random
secret = random.randint(1,20)
count = 1
print('---自己测试---')
temp = input('guess the number:')
guess = int(temp)
while guess != secret and count < 3:
        if guess > secret:
             print('too big')
        else:
             print('too small')
        temp = input('try again:')
        guess = int(temp)
        count 

TAG:

 

评分:0

我来说两句

日历

« 2024-03-29  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

  • 访问量: 9403
  • 日志数: 17
  • 建立时间: 2018-05-07
  • 更新时间: 2019-03-07

RSS订阅

Open Toolbar