String Concepts in Python: Indexing
String Concepts in Python: Indexing
Indexing
Indexing in Python allows you to access individual elements of a sequence (like
strings, lists, or tuples) using their position or "index." Each item in a sequence
has an index, starting with 0 for the first element, 1 for the second, and so on.
Negative indexing is also allowed, where -1 refers to the last element, -2 to the
second last, and so on.
Example of Indexing with a String:
name = "Python"
# Positive Indexing
print(name[0]) # Output: 'P'
print(name[3]) # Output: 'h'
# Negative Indexing
print(name[-1]) # Output: 'n' (last character)
print(name[-3]) # Output: 't'
In this example:
Untitled 1
Indexing is a powerful way to access and work with specific parts of
sequences in Python.
1. Creating Strings
Single-line String:
Multi-line String:
Example:
text = "Python"
print(text[0]) # Output: P
print(text[2]) # Output: t
Negative Indexing: You can also use negative indices to access characters
from the end of the string.
print(text[-1]) # Output: n
print(text[-3]) # Output: h
Untitled 2
3. Slicing Strings
Slicing allows you to get a specific part (substring) of the string.
Syntax: string[start:end]
Example:
word = "Python"
print(word[0:3]) # Output: Pyt
print(word[2:]) # Output: thon
print(word[:4]) # Output: Pyth
print(word[-3:]) # Output: hon
4. Modifying Strings
Strings are immutable in Python, meaning they cannot be changed after
creation. However, you can create a new string based on modifications.
Example:
text = "Hello"
new_text = text + " World" # Concatenation
print(new_text) # Output: Hello World
Untitled 3
print("hello".upper()) # Output: HELLO
.strip() : Removes whitespace from the beginning and end of the string.
6. Escape Characters
Escape characters allow you to include special characters in strings, like quotes
or newlines.
Examples:
Untitled 4
You can check for specific characters or substrings within a string using in and
not in keywords.
Example:
Practice Programs:
1. Write a program that takes a user’s full name as input and prints it in
uppercase.
2. Create a program that asks for a sentence and prints the number of times
the word "Python" appears in it.
Untitled 5
last_three_chars = user_string[-3:]
print("The last three characters are:", last_three_chars)
if statement:
if 5 > 3:
print("Five is greater than three")
if 3 > 5:
print("Three is greater")
else:
print("Five is greater")
age = 18
if age < 18:
print("You are a minor")
elif age == 18:
Untitled 6
print("You just turned 18!")
else:
print("You are an adult")
Practice Programs
1. Using if Statements
1. Write a program that asks the user for their age. If the user is 18 years or
older, print "You are an adult." Otherwise, do nothing.
2. Create a program that asks the user for a number. If the number is positive,
print "The number is positive."
Untitled 7
1. Write a Python program that checks whether a number entered by the user
is positive or negative. If the number is positive, print "Positive"; vv!, print
"Negative".
3. Write a program that asks the user to enter a number and checks if the
number is divisible by 5. If it is divisible, print "Divisible by 5"; otherwise,
print "Not divisible by 5."
Untitled 8
1. Create a program that asks the user for their marks and prints the grade
according to the following conditions:
2. Write a program that asks the user for the day of the week (as a number
from 1 to 7) and prints the name of the day (e.g., 1 for "Monday", 2 for
"Tuesday", etc.).
Untitled 9
elif day == 5:
print("Friday")
elif day == 6:
print("Saturday")
elif day == 7:
print("Sunday")
else:
print("Invalid day number")
3. Create a Python program that asks the user to enter a number between 1
and 100. If the number is greater than 90, print "High"; if it's between 50
and 90, print "Medium"; and if it's less than 50, print "Low."
Practice Questions
For if Statements
1. Write a program that asks the user to input their favorite color. If the color is
"blue", print "That's a cool color!".
2. Write a program that checks if a number entered by the user is greater than
100.
3. Write a program that asks the user for their score in a game. If the score is
above 50, print "You passed the level!".
4. Create a program that asks the user for the temperature in Celsius. If the
temperature is above 30, print "It's hot outside".
Untitled 10
5. Write a program to check if a given year is a leap year using the following
rule: a year is a leap year if it is divisible by 4 but not by 100, or it is divisible
by 400.
4. Create a program that asks the user for their score and checks if they
passed or failed (passing mark is 40).
2. Create a program that checks if a person’s weight falls into one of the
following categories:
3. Write a program to input the amount of rainfall (in mm) and print:
Untitled 11
Marks >= 50: Passed
For if Statements
1. Write a program that asks the user to input their favorite color. If the color
is "blue", print "That's a cool color!".
3. Write a program that asks the user for their score in a game. If the score is
above 50, print "You passed the level!".
Untitled 12
print("You passed the level!")
4. Create a program that asks the user for the temperature in Celsius. If the
temperature is above 30, print "It's hot outside".
Untitled 13
if number % 10 == 0:
print("Multiple of 10")
else:
print("Not a multiple of 10")
4. Create a program that asks the user for their score and checks if they
passed or failed (passing mark is 40).
Untitled 14
For if-elif-else Statements
1. Write a program that takes the price of a product as input and assigns a
discount based on the price.
3. Write a program to input the amount of rainfall (in mm) and print rainfall
classification.
Untitled 15
else:
print("Light Rain")
Untitled 16