当前位置:首页python > 正文

【内部资料】python把代码加上双引号和逗号

作者:野牛程序员:2023-12-20 17:10:00python阅读 2679
code_lines = """
    #include <stdio.h>
#include <math.h>

int main() {
    // 使用M_PI表示π
    double radius = 5.0;
    double circumference = 2.0 * M_PI * radius;

    // 打印圆的周长
    printf("圆的周长: %lf\\\\n", circumference);

    return 0;
}
"""

lines = code_lines.splitlines()
formatted_code = ',\\n'.join(['"' + line.replace('"', r'\\"').strip() + '",' for line in lines])
print(formatted_code)


修改后代码:

code_lines = """
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    // 初始化随机数发生器
    srand(time(NULL));

    // 生成1到100的随机整数
    int randomNumber = rand() % 100 + 1;

    // 输出随机数
    printf("随机数: %d\\n", randomNumber);

    return 0;
}




"""

lines = code_lines.splitlines()
formatted_code = '\\n'.join(['"' + line.replace('"', r'\\"') + '",' for line in lines])
print(formatted_code)


野牛程序员教少儿编程与信息学奥赛-微信|电话:15892516892
野牛程序员教少儿编程与信息学竞赛-微信|电话:15892516892
相关推荐

最新推荐

热门点击