字串印出 應用


#string index and scale str='hello python' print(str[0])#string index print(str[1:3])#string scale print(str[1::2])#third is step(default step is one) #use string scale to create new string print(str[0:2]+'s') #replace string str=str.replace('l', 'o')#use string replace l => o print(str) #find string begin=str.find('python')#it will return where the string begin place #if it can't find your word , it will return -1 print(str[begin:]) print('begin '+str[begin:]) #you can turn the string to list,to change to word newstr=list(str) print(newstr) newstr[0]='g'#change index 0 newstr=''.join(newstr)#all element join in one string #split the string #ex1 word='how are you?' print(word) word_split=word.split(' ')#use your keyword to split #ex2 word2='fine,thanks' word2_split=word2.split(',') print(word2_split) print(word_split) print(newstr)

0 意見:

張貼留言