python之Bug之字符串拼接bug

\r\n拼接Bug

环境: python3.4.pycharm2017
偶然的学习中遇到了一个问题,百思不得姐,什么问题呢,大家输入太快了,难免有失误就如下面的代码
    #构造响应数据
    response_start_line = "HTTP/1.1 200 OK0\r\n"
    response_headers = "Server: My server \r\n"
    # response_headers = "Server: My server\r \n"
    response_body = "hello darren"
    response = response_start_line  + response_headers + "\r\n" + response_body
    # response = response_start_line  + "\r \n " + response_headers + "\r\n" + response_body
    print("response data:%s" % response)

仔细一瞧没啥毛病,大家再看下图
在执行时如果是1和3的组合,打印的时候没毛病,会原样打出来,但是如果选择2和3组合,打出来的时候在response_headers这里只有一个空行没有数据。
所以我仔细看了看,\r\n之间多了个空格,为什么呢,但是如果\r\n在4中则不会影响打印结果,所以想了一想可能是规定,去查了下资料Windos的换行是\r\n,unix的是\n,mac的是\r,至于\r\n之间,我想是不能有空格的,至少在python里如此。

原文地址:https://www.cnblogs.com/hulichao/p/9656700.html