0% found this document useful (0 votes)
2 views9 pages

10.1 String Examples

The document provides a series of Python examples demonstrating various string operations, including concatenation, length, character access, slicing, whitespace removal, substring search, and formatting. It also includes practical applications like username generation, password checking, and phone number cleaning. Each example is accompanied by code snippets and expected output.

Uploaded by

aleezayf588
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

10.1 String Examples

The document provides a series of Python examples demonstrating various string operations, including concatenation, length, character access, slicing, whitespace removal, substring search, and formatting. It also includes practical applications like username generation, password checking, and phone number cleaning. Each example is accompanied by code snippets and expected output.

Uploaded by

aleezayf588
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

String Examples (Python)

Question 1: String Concatenation

--------------------------------

# Combine first_name and last_name with a space in between

first_name = "Jane"

last_name = "Smith"

full_name = first_name + " " + last_name

print(full_name)

Output:

Jane Smith

Question 2: String Length

--------------------------------

# Find and print the length of this string

message = "Hello Python"

print(len(message))

Output:

12

Question 3: Accessing Characters

--------------------------------
# Print the first and last character of the string

text = "Programming"

print(f"First: {text[0]}, Last: {text[-1]}")

Output:

First: P, Last: g

Question 4: String Slicing

--------------------------------

# Extract "Python" from this string

text = "I love Python programming"

print(text[7:13])

Output:

Python

Question 5: Removing Whitespace

--------------------------------

# Remove the extra spaces from beginning and end

text = " Hello World "

print([Link]())

Output:
Hello World

Question 6: Find Substring

--------------------------------

# Check if "World" exists in the string

text = "Hello World"

print("World" in text)

Output:

True

Question 7: Check String End

--------------------------------

# Check if filename ends with .py

filename = "[Link]"

print([Link](".py"))

Output:

True

Question 8: Count Characters

--------------------------------

# Count how many times 'l' appears in the string


text = "Hello World"

print([Link]("l"))

Output:

Question 9: String Formatting with f-strings

--------------------------------

# Create a sentence using variables

name = "Alice"

age = 25

city = "New York"

print(f"{name} is {age} years old and lives in {city}")

Output:

Alice is 25 years old and lives in New York

Question 10: Extract Domain from Email

--------------------------------

# Get everything after/before @ symbol

email = "user@[Link]"

print([Link]("@")[1])

print([Link]("@")[0])
Output:

[Link]

user

Question 11: Check Palindrome

--------------------------------

# Check if string reads the same backwards

word = "radar"

print(word == word[::-1])

Output:

True

Question 12: Remove Vowels

--------------------------------

# Remove all vowels (a, e, i, o, u) from the string

text = "Hello World"

vowels = "aeiouAEIOU"

result = ""

for word in text:

if word not in vowels:

result = result+char

print(result)
Output:

Hll Wrld

Question 13: Count Words

--------------------------------

# Count the number of words in the sentence

sentence = "Python is a powerful language"

print(len([Link]())) #here split by space taking default space

Output:

Question 14: Check Empty String

--------------------------------

# Check if string is empty

text1 = ""

text2 = "Hello"

print(len(text1) == 0)

print(len(text2) == 0)

# Simpler way:

print(not text1)

print(not text2)
Output:

True

False

True

False

Question 15: Get Middle Character

--------------------------------

# Get the middle character of the string

text = "Python"

middle = len(text) // 2

print(text[middle])

Output:

t
Combined Examples

--------------------------------

Username Generator

# Create username from email

email = "[Link]@[Link]"

username = [Link]("@")[0]

print(f"Username: {username}")

Output:

Username: [Link]

Simple Password Checker

# Check password length

password = "secret123"

if len(password) >= 8:

print("Strong password")

else:

print("Too short")

Output:

Strong password

Clean Phone Number

# Remove dashes from phone number

phone = "123-456-7890"
clean = [Link]("-", "")

print(clean)

Output:

1234567890

First Letter of Each Word

# Get initials

name = "John Michael Smith"

initials = name[0] + name[5] + name[12]

print(initials)

Output:

JMS

Check if All Letters

# Validate name (only letters)

name = "Alice123"

if [Link]():

print("Valid name")

else:

print("Contains numbers")

Output:

Contains numbers

You might also like