1.
Strings and Concatenation
# String Concatenation
str1 = "hello"
str2 = "world"
result = str1 + str2
print("Concatenated String:", result)
# Output: helloworld
2. Length of a String
str = "Apna College"
print("Length of the string:", len(str))
# Output: 12
3. Indexing
str = "Apna_College"
print("First character:", str[0]) # A
print("Fourth character:", str[3]) # a
# Note: Strings are immutable. str[0] = 'B' will cause an error.
4. Slicing
str = "ApnaCollege"
print(str[1:4]) # pna
print(str[:4]) # Apna
print(str[1:]) # pnaCollege
# Negative indexing
str2 = "Apple"
print(str2[-3:-1]) # pl
5. String Functions
str = "I am a coder."
print(str.endswith("er.")) # True
print(str.count("am")) # 1
print(str.capitalize()) # I am a coder.
print(str.find("coder")) # 7
print(str.replace("coder", "programmer")) # I am a programmer.
6. Practice: Length of First Name
name = input("Enter your first name: ")
print("Length of your name is:", len(name))
7. Practice: Count Occurrence of '$'
str = input("Enter a string: ")
print("Occurrences of '$':", str.count('$'))
8. Conditional Statements - Syntax
num = 10
if num > 0:
print("Positive number")
elif num == 0:
print("Zero")
else:
print("Negative number")
9. Grading System Based on Marks
marks = int(input("Enter your marks: "))
if marks >= 90:
grade = "A"
elif marks >= 80:
grade = "B"
elif marks >= 70:
grade = "C"
else:
grade = "D"
print("Your grade is:", grade)
10. Check Odd or Even
num = int(input("Enter a number: "))
if num % 2 == 0:
print("Even")
else:
print("Odd")
11. Greatest of 3 Numbers
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
if a >= b and a >= c:
print("Greatest is:", a)
elif b >= a and b >= c:
print("Greatest is:", b)
else:
print("Greatest is:", c)
12. Check Multiple of 7
num = int(input("Enter a number: "))
if num % 7 == 0:
print("It is a multiple of 7")
else:
print("Not a multiple of 7")