🔁 50+ Python String Methods & Examples
🔁 50+ Python String Methods & Examples 📝
1. lower() – Convert string to lowercase
2. upper() – Convert string to uppercase
3. strip() – Remove leading & trailing whitespace
4. lstrip() – Remove leading spaces
5. rstrip() – Remove trailing spaces
6. replace() – Replace a substring
7. split() – Split string into a list
8. rsplit() – Split from the right
9. splitlines() – Split string by lines
10. join() – Join list into string
11. find() – Find substring index (-1 if not found)
12. rfind() – Find substring from the right
13. index() – Find substring index (raises error if not found)
14. rindex() – Find substring index from right
15. count() – Count occurrences of a substring
16. startswith() – Check string prefix
17. endswith() – Check string suffix
18. isdigit() – Check if all characters are digits
19. isalpha() – Check if all characters are letters
20. isalnum() – Check if alphanumeric
21. isupper() – Check if all letters are uppercase
22. islower() – Check if all letters are lowercase
23. istitle() – Check if string is title cased
24. isspace() – Check if string is whitespace
25. title() – Convert string to title case
26. capitalize() – Capitalize first letter
27. swapcase() – Swap upper/lower case
28. center() – Center string with padding
29. ljust() – Left justify with padding
30. rjust() – Right justify with padding
31. zfill() – Pad numeric string with zeros
32. partition() – Split string into tuple by separator
33. rpartition() – Partition from right
34. expandtabs() – Replace tabs with spaces
35. encode() – Encode string to bytes
36. decode() – Decode bytes to string
37. format() – Insert values into string
38. format_map() – Format using a dictionary
39. encode() – Encode string in specified encoding
40. removeprefix() – Remove starting substring (Python 3.9+)
41. removesuffix() – Remove ending substring (Python 3.9+)
42. translate() – Replace characters using mapping
43. maketrans() – Create translation mapping
44. casefold() – Aggressive lowercasing for comparisons
45. removeprefix() – Remove prefix string
46. removesuffix() – Remove suffix string
47. isascii() – Check if all chars are ASCII
48. partition() – Split string into three parts
49. rpartition() – Split string from right
50. removeprefix() – Remove specific prefix
51. removesuffix() – Remove specific suffix
52. expandtabs() – Convert tab to spaces
💡 Tips:
-
Use
strip(),lstrip(),rstrip()to clean input. -
Combine
split()&join()for powerful text processing. -
Use
format()or f-strings for dynamic strings. -
Use
translate()andmaketrans()for bulk character replacements.
Comments
Post a Comment