I have a string like below
s = '''printf("\nFloat value is %f \n", flt);
printf("Integer value is %d\n" , no);
printf("Double value is %lf \n", dbl);
printf("\nOctal value is %o \n", no);
printf("Hexadecimal value is %x \n", no);
return 0;'''
I want to split the string. I used splitlines()
But it's not giving the output I'm expected. It also splitting the string if it contain \n
in middle of the string. What i want is I want to ignore the newline characters in middle of the strings while splitting the string. Is there any way can I solve this without using the files.
for line in s.splitlines():
#line = somfunction(line)
print(line)
Output of above code
printf("
Float value is %f
", flt);
printf("Integer value is %d
" , no);
printf("Double value is %lf
", dbl);
printf("
Octal value is %o
", no);
printf("Hexadecimal value is %x
", no);
return 0;
Expected Output:
printf("\nFloat value is %f \n", flt);
printf("Integer value is %d\n" , no);
printf("Double value is %lf \n", dbl);
printf("\nOctal value is %o \n", no);
printf("Hexadecimal value is %x \n", no);
return 0;
You can achieve the desired result using a string raw notation in your s
definition.
s = r'''printf("\nFloat value is %f \n", flt);
printf("Integer value is %d\n" , no);
printf("Double value is %lf \n", dbl);
printf("\nOctal value is %o \n", no);
printf("Hexadecimal value is %x \n", no);
return 0;'''
Notice the r
in leading of string. In python this mean a string raw.
Here is the documentation
Try this
s = r'''printf("\nFloat value is %f \n", flt);
printf("Integer value is %d\n" , no);
printf("Double value is %lf \n", dbl);
printf("\nOctal value is %o \n", no);
printf("Hexadecimal value is %x \n", no);
return 0;'''
for line in s.splitlines():
print(line)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.