fork download
  1. s = "Hey therea ahat shou"
  2.  
  3. # Length should be 20
  4. print "Length of s = %d" % len(s)
  5.  
  6. # First occurrence of "a" should be at index 8
  7. print "The first occurrence of the letter a = %d" % s.index("a")
  8.  
  9. # Number of a's should be 2
  10. print "a occurs %d times" % s.count("a")
  11.  
  12. # Slicing the string into bits
  13. print "The first five characters are '%s'" % s[:5] # Start to 5
  14. print "The next five characters are '%s'" % s[5:10] # 5 to 10
  15. print "The twelfth character is '%s'" % s[12] # Just number 12
  16.  
  17. print "The last five characters are '%s'" % s[-5:] # 5th-from-last to end
  18.  
  19. # Convert everything to uppercase
  20. print "String in uppercase: %s" % s.upper()
  21.  
  22. # Convert everything to lowercase
  23. print "String in lowercase: %s" % s.lower()
  24.  
  25. # Check how a string starts
  26. if s.startswith("Str"):
  27. print "String starts with 'Str'. Good!"
  28.  
  29. # Check how a string ends
  30. if s.endswith("ome!"):
  31. print "String ends with 'ome!'. Good!"
  32.  
  33. # Split the string into three separate strings,
  34. # each containing only a word
  35. print "Split the words of the string: %s" % s.split(" ")
Success #stdin #stdout 0.03s 6356KB
stdin
Standard input is empty
stdout
Length of s = 20
The first occurrence of the letter a = 9
a occurs 3 times
The first five characters are 'Hey t'
The next five characters are 'herea'
The twelfth character is 'h'
The last five characters are ' shou'
String in uppercase: HEY THEREA AHAT SHOU
String in lowercase: hey therea ahat shou
Split the words of the string: ['Hey', 'therea', 'ahat', 'shou']