cfabio

String.py

Jan 5th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.73 KB | None | 0 0
  1. r'That is Carol\s cat' #raw string. The slash are interprent as part of the string
  2.  
  3. #Till the next triple quotes everything is interpreted
  4. #as string (backslashes, new line...). Nothing needs to be escaped
  5. """Dear...
  6. Today I've been....
  7. Regards,
  8. Fabio"""
  9.  
  10. ##########################
  11. #strings: similar to list
  12. name[0]
  13. name[1:4}
  14. name[-1]
  15.  
  16. 'fa' in name
  17.  
  18. for i in name :
  19.     print(name[i])
  20.  
  21. ##but string are immutable in python!! They cannot be changed. To modify a string
  22. ##I need to create a new string
  23. name = 'Fabio'
  24. newName = name + ' Ciampi'
  25.  
  26. ##########################
  27. count = {}
  28. pprint.pprint(count)
  29. pprint.pformat(count) #returns a string, instead of printing
  30.  
  31. ####################
  32.  
  33. spam = 'Hello'
  34. spam = spam.upper()
  35. spam.isupper()
  36. 'Hello'.isupper()
  37.  
  38. isalpha()
  39. isdecimal()
  40. istitle()
  41.  
  42. 'Ciao Fabio'.startswith('Fab')
  43. 'Ciao Fabio'.endswith('Fab')
  44.  
  45. #concatenation
  46. spam = 'hello' + ' fabio'
  47.  
  48. #join strings, using the provided separator
  49. ','.join('uno', 'due', 'tre')
  50.  
  51. #split strings
  52.  myList = 'My name is Fabio'.split()
  53.  myList = 'My name is Fabio'.split('m')
  54.  
  55. #padding
  56. 'Hello'.rjust(10) #pad the string to make it lenght 10
  57. 'Hello'.ljust(10) #pad the string to make it lenght 10
  58. 'Hello'.rjust(10, '*') #define the padding character
  59. 'Hello'.center(10)
  60.  
  61. #stripping
  62. spam.strip() #returns a string without spaces
  63. spam.lstrip()
  64. spam.rstrip('=') #remove from the right one specific char
  65.  
  66. #replace
  67. spam = 'Hello world'
  68. spam.replace('e', 'bla')
  69.  
  70.  
  71. #copy/paste to clipboard
  72. import pyperclip
  73. pyperclip.copy("Hello") #writes to the clipboard
  74. pyperclip.paste()       #get the content of the clipboard
  75.  
  76. #string formatting
  77. name = 'fabio'
  78. local = 'johnny'
  79. 'Hello %s, do you come at %s?' % (name, local)
Add Comment
Please, Sign In to add comment