无法在Python中连接字符串和整数的原因

时间:2017-05-13 18:12:30

标签: python string int operator-overloading concatenation

很多文档都证明,在将int连接到字符串之前需要str才能连接:

'I am ' + str(n) + ' years old.'

Python必须有一个根本原因

'I am ' + n + ' years old.'

我想知道这是什么原因。在我的项目中,我打印了很多数字并最终得到了这样的代码。

'The GCD of the numbers ' + str(a) + ', ' + str(b) + ' and ' + str(c) + ' is ' + str(ans) + '.'

如果我能放弃'那将会更漂亮。这就是让我的经历特别烦恼的原因。我和SymPy一起工作,所以我有这样的代码:

'Consider the polynomial ' + (a*x**2 + b*x + c)

由于我们超载了' +'对于SymPy表达式和字符串。但是我们无法对整数执行此操作,因此,当a = b = 0且多项式减少为整数常数时,此代码会失败!所以对于那个离群值的情况,我被迫写了:

'Consider the polynomial ' + str(a*x**2 + b*x + c)

再次,解决方案'字符串' + str(int)很简单,但我的帖子的目的是通过Python理解不允许'字符串' + int的方式,例如,Java。

2 个答案:

答案 0 :(得分:1)

它与+运算符有关。

使用a运算符时,根据与运算符结合使用的boperator.concat(a, b)变量类型,可能会调用两个函数:

operator.add(a, b).concat(a, b)

a + b方法会为序列返回.add(a, b)

a + b方法会为数字返回+

这可以在pydocs here中找到。

Python不会隐式转换变量类型,以便函数“有效”。这对代码来说太混乱了。因此,您必须满足#define TAMANHO_ARQUIVO 10 // main file size #define QTD_DISCOS 3 // number of files to divide the main file int main () { FILE *arquivoPrincipal; // main file int x = 0; int incpos=0; char name[FILENAME_MAX]; int memoria_interna = (int) ceilf(TAMANHO_ARQUIVO/QTD_DISCOS); char vet[256] ; printf("memoria_interna:%d remainder:%d \n",memoria_interna,TAMANHO_ARQUIVO%QTD_DISCOS); int remainder = TAMANHO_ARQUIVO%QTD_DISCOS; arquivoPrincipal = fopen("arquivo.txt", "w+"); int isLastDisco = memoria_interna-1; srand(time(NULL)); for (int i = 0; i < TAMANHO_ARQUIVO; ++i) { x = rand() % 10; fprintf(arquivoPrincipal, "%d\n", x); } fseek(arquivoPrincipal, 0 , SEEK_SET);/*reseta para a posição zero*/ for (int i = 0; i < QTD_DISCOS; ++i) { FILE *arquivo; int numOfIntsTosave; int *pInts; sprintf(name,"disco_%d.txt", i); // generating files names dynamically printf("%s\n", name); memset(vet, 0, 256); // clearing vector //qtd_int = fread(vet, sizeof(char), memoria_interna*2, arquivoPrincipal); // reading main file to get number of items from 0 to memoria_interna (which is the size of the main file / the number of files to be divided) arquivo = fopen(name, "w"); if (!arquivo) { printf("Error!\n"); exit(0); } if(i==QTD_DISCOS-1)/*If we are in the last iteration of the for loop, then if there is a remainder, we add to memoria_interna */ numOfIntsTosave = memoria_interna+remainder;/*remainder this can be zero*/ else numOfIntsTosave = memoria_interna; pInts = (int*)malloc(sizeof(int)*numOfIntsTosave);/*alloc buffer to int´s*/ if(pInts==NULL){ printf("error malloc\n"); exit(0); } for(int u=0;u<numOfIntsTosave;u++) { fscanf(arquivoPrincipal,"%d\n",&pInts[u]);/*le do arquivo principal*/ fprintf(arquivo, "%d\n", pInts[u]);/*salva no arquivo*/ incpos+=2;/* um int e um /n */ } if(pInts) free(pInts);/*free buffer of int´s*/ incpos+=2;/* um int e um /n */ //fwrite(vet, sizeof(char), qtd_int , arquivo);//printf("Error!\n"); fclose(arquivo); /*seeking/seting the position (for the next loop)*/ fseek(arquivoPrincipal,incpos, SEEK_SET); } fclose(arquivoPrincipal); return 0; } 运算符的要求才能使用它。

答案 1 :(得分:1)

您可以使用&#34;,&#34; 代替&#34; +&#34;

a=4
x=5
b=6
c=5
n=100
my_age="I am",n,"Years old"
print("I may say",my_age)

print('Consider the polynomial ' ,(a*x**2 + b*x + c))