Python String

🔤 Python Strings – 20 Basic Assignments


1) Create & Print a String

Ask the user to input a sentence and print it.


2) Access Characters

Ask user for a word and print the first and last character.


3) String Length

Ask user for input and print its length (len()).


4) Uppercase Conversion

Convert a user’s string to uppercase and print.


5) Lowercase Conversion

Convert a user’s string to lowercase and print.


6) Capitalize

Ask for a sentence and print it with the first letter capitalized.


7) Slice a Substring

Ask user for a word and print characters from index 1 to 3.


8) Reverse a String

Ask user for a word and print it backwards using slicing.

Example:


9) Check Substring

Ask the user for a sentence and a word — check if the word is in the sentence.


10) Replace a Substring

Ask for a sentence, then replace all occurrences of "a" with "@" and print.


11) Count Characters

Ask for a sentence and count how many times “e” appears.


12) Split on Space

Ask for a sentence and split it into words using split().


13) Join Words

Given a list of words:

Join into a single string with spaces.


14) Find Index

Ask for a word and a character — print the index of the first occurrence of that character.


15) Strip Spaces

Ask the user for a string with extra spaces at the ends. Use strip() to remove them and print.


16) String Formatting

Ask for name and age and print:


17) Count Words

Ask for a sentence and count how many words it has.


18) Check Palindrome

Ask for a word. If it reads the same backward, print “Palindrome”, else “Not Palindrome”.


19) First 3 Characters

Ask user for a word and print its first 3 characters.

If it’s shorter than 3, print the whole word.


20) Replace Spaces with Hyphens

Ask for a sentence and convert spaces " " to hyphens "-".

Example:


📝 Notes for Students

Common string operations you’ll use:

Operation
Example Function

Length

len(s)

Uppercase

s.upper()

Lowercase

s.lower()

Slice

s[1:4]

Reverse

s[::-1]

Find

s.find('a')

Replace

s.replace('a', '@')

Strip

s.strip()

Split

s.split()


canvil: 333f6c7d-6876-4f7e-b6ad-1bdb2233f5c1

Last updated