Sitemap

Member-only story

Strings in Python [2]

3 min readDec 13, 2024

--

Hello,

Press enter or click to view image in full size
Photo by Kevin Canlas on Unsplash

41. Check if String Contains Only Digits

python
Copy code
only_digits = "12345".isdigit()  # True

42. Convert String to List of Words

python
Copy code
words = "Hello world, Python is great!".split()  # ['Hello', 'world,', 'Python', 'is', 'great!']

43. Check if String is Printable

python
Copy code
is_printable = "Hello\nWorld".isprintable()  # False (contains non-printable newline character)

44. Left-Justify a String

python
Copy code
left_justified = "Hello".ljust(10, "-")  # 'Hello-----'

45. Right-Justify a String

python
Copy code
right_justified = "Hello".rjust(10, "-")  # '-----Hello'

46. Remove Characters from Both Ends

python
Copy code
stripped = "---Hello---".strip("-")  # 'Hello'

--

--