Advertisement
Felanpro

Pretty much all about strings!

Mar 3rd, 2016
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. #---!PRETTY MUCH ALL ABOUT STRINGS!---PYTHON VERSION: 3.3.3  IDLE VERSION: 3.3.3
  2. string1 = "hello world!"
  3. string2 = "  hello world!"  #Meant for strip() function
  4.  
  5. print(string1[0:4])
  6. #Retrieve the first four characters of the string.
  7.  
  8. print(string1[2:5])
  9. #Retrieve the characters between index 2 to index 5 in the string.
  10.  
  11. print(string1[:-5])
  12. #Retrieve the last 5 characters of the string.
  13.  
  14. print("%s" % (string1))
  15. #String Formatting  (You can also do %c for character and %d for integer.)
  16.  
  17. print(string1.capitalize())
  18. #Will capitalize the first character in a string.
  19.  
  20. print(string1.find("world"))
  21. '''
  22. Will find the word world and output the index number of the word's first
  23. character.
  24. '''
  25. print(string1.isalpha())
  26. #Returns true if all characters are letters in the string.
  27.  
  28. print(string1.isalnum())
  29. #Returns true if all characters are numbers.
  30.  
  31. print(len(string1))
  32. #Returns the length of the string.
  33.  
  34. print(string1[:4] + " noooo")
  35. #Prints out the first four letters of the string and then adds the the word noo.
  36.  
  37. print(string1.replace("hello", "hey"))
  38. #Replaces the word hello with hey.
  39.  
  40. print(string2.strip())
  41. #Remove white space from front and end.
  42.  
  43. list1 = string1.split(" ")
  44. print(list1)
  45. #Split a string into a list based on the delimiter you provide.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement