Reputation: 9061
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;
Upvotes: 1
Views: 366
Reputation: 73
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)
Upvotes: 0
Reputation: 3270
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
Upvotes: 1