python 字元串 拼接問題

f=open('A.txt')
d=f.readlines()
f.close()

foriinrange(0,len(d),2):
print"%s:%s"%(d[i].strip(),d[i+1].strip())

結果:
192.168.1.1:8080
192.168.1.4:8081
192.168.5.1:80

㈡ python字元串怎麼和整數連接

1、在python中完成字元串復和數字的拼接制,可以使用內置函數str()。

㈢ python中字元串拼接

if__name__=='__main__':
result=''
data=['num1','num2','num3','num4']
foriinrange(len(data)):
result+='OR'+'''+data[i]+'''
print(result)

㈣ Python字元串拼接的幾種方法

Python字元串拼接的幾種方法(python3.5):
1、str1+str2
使用+號進行字元串拼接:'wbz'+'ctt'='wbzctt'
2、str1,str2
這種方式有點特殊,如果兩個字元串用逗號隔開,那麼兩個字元串就會被拼接,嚴格講不
叫拼接:'wbz','ctt'=('wbz』,'ctt')
3、str1str2
這種拼接方式是Python獨有的,只要將兩個字元串放在一起,這兩個字元串就會自動拼接
成新的字元串,不管這兩個字元串中間是否存在空格:'wbz''ctt'='wbzctt'
'wbz''ctt'='wbzctt'
4、%連接字元串
這種方式相對於其他的拼接方式來說就有些強大了,因為它借鑒了C語言中printf()函數
的功能。這種方式用符號'%'連接一個字元串和一組變數,字元串中的特殊標記會被自動用
右邊變數組中的變數替換:'%s%s'%('wbz','ctt')='wbzctt'
5、字元串列表連接str.join(list)
這個函數join接受一個列表,並用字元串連接列表中的每一個元素:
data=['wbz','ctt','Python']
str='**##'
str.join(data)='wbz@@@ctt@@@Python'
6、字元串乘法
這種方法也是可以進行字元串拼接的,但是這種方式是不經常使用的:
str='Python'
str*2='PythonPython'

㈤ 在python中怎麼連接變數和字元串

python中變數和字元串的連接
字元串中加含變數,一定要內外包圍相同版。
如下輸出權:
table='abc'

str1='select * from '+table+''
str2="select * from "+table+""
str3='select * from "'+table+'"'
str4="select * from '"+table+"'"

print 'str1 is :',str1
print 'str2 is :',str2
print 'str3 is :',str3
print 'str4 is :',str4
輸出結果:
[python] view plain
str1 is : select * from abc
str2 is : select * from abc
str3 is : select * from "abc"
str4 is : select * from 'abc'

㈥ python用字元串拼接一條語句,然後怎麼執行

defcutbody(*args):
printargs[0][args[1]:args[2]]
cutbody('11111',2,3)

改成這樣可能會簡便一點吧,希望能幫到你~

㈦ python 如何進行多個字元串的拼接

A="A"
B="B"
C="C"
1. A+B+C

2. "In the basket are %s, %s and %s" % (A,B,C)

㈧ python字元串連接的幾種方式總結

1、相襲加
website = 'python' + 'tab' + '.com'
2、%
'my name is %s,now %d years old' % ('liming',27)
3、{}.format
'myname is {0},now {1} years old'.format('liming','27')

㈨ python如何輸出自己需要的字元串以及連接的方式

1. 使用 '+' 連接
例如 s = s+ 'hello world!抄'
這種襲方式最慢
2. 使用 ' +='
例如 s += 'hello wold'
這種方式與第一種作用相同,但是效率高一點
3.使用 ".join(list)"
這種方式效率最高
使用時可以先用一個list緩存字元串,然後使用join方法,得到最終結果
4.python的字元串格式化操作符 %
例如 s = '%s New %s !'%('Happy','Year')
這種方式會讓代碼的可讀性更好,也不用對非字元串調用str方法

㈩ Python字元串拼接的幾種方法整理

1、相加
website = 'python' + 'tab' + '.com'
2、%
'my name is %s,now %d years old' % ('liming',27)
3、{}.format
'myname is {0},now {1} years old'.format('liming','27')