社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

在Python中将\n用作普通字符串

obami • 3 年前 • 1416 次点击  

我正在用数字做一个字符串计算器。例如: 154 + 246 = 154246

因此,用户将输入一组数字,使用 \n .如你所知, \n 用于生成新行,但我需要使用at作为任何普通字符串。我需要使用 \n

代码:

num_list = []
# this function will add a number to the list
def ask_num():
    # ask for a number (we will make it a string so we can add comma and /n)
    num = input("Enter numbers: ")

    
# run the function for asking numbers
ask_num()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/129236
 
1416 次点击  
文章 [ 3 ]  |  最新文章 3 年前
obami
Reply   •   1 楼
obami    3 年前

成功的解决方案:

def ask_num():
    num_list = []
    # ask for a number (we will make it a string so we can add comma and /n)
    num = input("Enter numbers: ")
    # put the num in the list
    num_list.append(num)
    # seperate the numbers
    num_list = num.split(r"\n")
    # print the list
    print(num_list)
FLAK-ZOSO
Reply   •   2 楼
FLAK-ZOSO    3 年前

你可以 逃跑 这个 \n 使用另一个 \

>>> print("\\n")
\n
>>> print("\n")


>>> print("/n")
/n

在你的情况下,你要求 /n ,它不用于换行符,因此通常可以在字符串中使用它。

Don CorliJoni
Reply   •   3 楼
Don CorliJoni    3 年前

你可以用

r"\n"

"\\n"