`

Python String Operation Basics

阅读更多

    First of all,python strings can not be changed,because they are immutable.

  • String escape
'Isn\'t,he said'
"Isn\"t,he said"

 

  • more readable string

   

print("Isn\"t,he said")
  •  do not want to escape
print(r'C:some\name')
 
  •  String concatenating

 

"py" + "pyon"
"py" "pyon" #works
prefix="py"
prefix "thon" #does't work
3 * "un" "beat" #does't work
prefix + (3 * "un") #works

 

  •  String indexing
word="python"
word[0] #'p'
word[-1] #'n'
  •  String slicing
word="python"
word[0:2] #'py'

 

  •  String length

     the build-in function len() returns the length of a string.

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics