fork download
  1. # your code goes here
  2. # String Functions in Python
  3.  
  4. # Sample string
  5. s = " Hello, World! 123 "
  6.  
  7. # Basic String Operations
  8. print("Original String:", s)
  9. print("Length:", len(s)) # Length of the string
  10. print("Uppercase:", s.upper()) # Convert to uppercase
  11. print("Lowercase:", s.lower()) # Convert to lowercase
  12. print("Title Case:", s.title()) # Convert to title case
  13. print("Swap Case:", s.swapcase()) # Swap uppercase/lowercase
  14. print("Capitalized:", s.capitalize()) # Capitalize first letter
  15.  
  16. # Stripping whitespaces
  17. print("Stripped:", s.strip()) # Remove leading and trailing spaces
  18. print("Left Stripped:", s.lstrip()) # Remove leading spaces
  19. print("Right Stripped:", s.rstrip()) # Remove trailing spaces
  20.  
  21. # Searching in strings
  22. print("Find 'World':", s.find("World")) # Find substring index
  23. print("Index of 'o':", s.index("o")) # Get first occurrence index
  24. print("Count of 'l':", s.count("l")) # Count occurrences
  25.  
  26. # String Checks
  27. print("Starts with ' Hello':", s.startswith(" Hello")) # Check start
  28. print("Ends with '123 ':", s.endswith("123 ")) # Check end
  29. print("Is Alphanumeric:", s.isalnum()) # Check if alphanumeric
  30. print("Is Alphabetic:", s.isalpha()) # Check if only alphabets
  31. print("Is Numeric:", s.isdigit()) # Check if only digits
  32. print("Is Lowercase:", s.islower()) # Check if all lowercase
  33. print("Is Uppercase:", s.isupper()) # Check if all uppercase
  34. print("Is Title:", s.istitle()) # Check if title case
  35. print("Is Space:", s.isspace()) # Check if only spaces
Success #stdin #stdout 0.02s 7384KB
stdin
Standard input is empty
stdout
('Original String:', ' Hello, World! 123 ')
('Length:', 19)
('Uppercase:', ' HELLO, WORLD! 123 ')
('Lowercase:', ' hello, world! 123 ')
('Title Case:', ' Hello, World! 123 ')
('Swap Case:', ' hELLO, wORLD! 123 ')
('Capitalized:', ' hello, world! 123 ')
('Stripped:', 'Hello, World! 123')
('Left Stripped:', 'Hello, World! 123 ')
('Right Stripped:', ' Hello, World! 123')
("Find 'World':", 8)
("Index of 'o':", 5)
("Count of 'l':", 3)
("Starts with ' Hello':", True)
("Ends with '123 ':", True)
('Is Alphanumeric:', False)
('Is Alphabetic:', False)
('Is Numeric:', False)
('Is Lowercase:', False)
('Is Uppercase:', False)
('Is Title:', True)
('Is Space:', False)