
![]() |
@wtf | |
(Word counter & palindrome checker) |
||
1
Replies
6
Views
1 Bookmarks
|
![]() |
@wtf | 26 days |
Now, let's move to the next topic in the Python Coding Challenge: *Strings & String Methods* A string is a sequence of characters inside quotes. You can use: name = Alice greeting = 'Hello!' paragraph = This is a multiline string. Strings are immutable once created, they can't be changed directly. *Common String Methods* *Here are some useful methods youll use all the time:* lower() &8594; makes everything lowercase upper() &8594; makes everything uppercase strip() &8594; removes spaces from start and end replace(old, new) &8594; replaces parts of a string split() &8594; splits text into a list of words count(word) &8594; counts how many times something appears find(word) &8594; finds the position of a word startswithHello.gif &8594; checks how a string begins endswith(world) &8594; checks how a string ends *Examples* msg = Python is Awesome! print(msg.lower()) python is awesome! print(msg.strip()) Python is Awesome! print(msg.replace(Awesome, Powerful)) Python is Powerful! print(msg.split()) ['Python', 'is', 'Awesome!'] *Project 1: Word Counter* text = input(Enter a sentence: ) words = text.split() print(Word count:, len(words)) Try it with: Python is easy and powerful &8594; Output: 5 *Project 2: Palindrome Checker* text = input(Enter a word: ) if text == text[::-1]: print(Palindrome!) else: print(Not a palindrome.) Try: - madam &8594; Palindrome - racecar &8594; Palindrome - hello &8594; Not a palindrome |
||


