0% found this document useful (0 votes)
4 views18 pages

Strings in Python

The document provides a detailed guide on strings in Python, covering their definition, creation methods (single, double, and triple quotes), and various operations such as indexing, slicing, concatenation, and common string methods. It also discusses string immutability, formatting, and practical examples like username and palindrome checkers. Additionally, it includes practice questions and notes on important string methods used in programming.

Uploaded by

lekhmalvi03
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)
4 views18 pages

Strings in Python

The document provides a detailed guide on strings in Python, covering their definition, creation methods (single, double, and triple quotes), and various operations such as indexing, slicing, concatenation, and common string methods. It also discusses string immutability, formatting, and practical examples like username and palindrome checkers. Additionally, it includes practice questions and notes on important string methods used in programming.

Uploaded by

lekhmalvi03
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

5/22/26, 7:17 PM Strings in Python

strings in python in details

Strings in Python (Detailed Guide)


A string in Python is a sequence of characters used to store text.

Examples:

name = "Vansh"
city = 'Betul'
Python
sentence = "Python is easy" Run

Strings can contain:


Letters
Numbers
Symbols
Spaces
Emojis

Creating Strings
1. Double Quotes

a = Python
"Hello" Run

2. Single Quotes

b = Python
'Python' Run

3. Triple Quotes
Used for multiline strings.

[Link] 1/18
5/22/26, 7:17 PM Strings in Python
c = """This is
Python Run
multi-line
string"""

Checking Data Type

name = "Vansh"

Python
print(type(name)) Run

Output:

Python
<class 'str'> Run

String Indexing
Each character has a position number.

Example:

textPython
= "Python" Run

Character P y t h o n

Index 0 1 2 3 4 5

Access character:

Python
print(text[0]) Run

Output:

P Python Run

[Link] 2/18
5/22/26, 7:17 PM Strings in Python

Negative Indexing
Python also supports negative indexing.

Character P y t h o n

Negative -6 -5 -4 -3 -2 -1
Index

Example:

Python
print(text[-1]) Run

Output:

n Python Run

String Slicing
Used to get part of a string.

Syntax:

Python
string[start:end] Run

Example:

text = "Python"

Python
print(text[0:3]) Run

Output:

Pyt Python Run

Explanation:
Start included

[Link] 3/18
5/22/26, 7:17 PM Strings in Python

End excluded

Slicing Examples

text = "Programming"

print(text[0:4]) # Prog
print(text[4:7]) # ram
print(text[:5]) # Progr
print(text[3:]) # gramming
Python
print(text[-4:]) # ming Run

Step in Slicing
Syntax:

Python
string[start:end:step] Run

Example:

text = "Python"

Python
print(text[0:6:2]) Run

Output:

Pto Python Run

Reverse a String

text = "Python"

Python
print(text[::-1]) Run

[Link] 4/18
5/22/26, 7:17 PM Strings in Python

Output:

Python
nohtyP Run

String Concatenation
Joining strings using +

first = "Vansh"
last = "Malvi"

full = first + " " + last

Python
print(full) Run

Output:

Python
Vansh Malvi Run

String Repetition

Python" * 3)
print("Hi Run

Output:

Python
Hi Hi Hi Run

Length of String

text = "Python"

Python Run
[Link] 5/18
5/22/26, 7:17 PM Strings in Python
yt o u
print(len(text))

Output:

6 Python Run

Membership Operators
Check if text exists

text = "Python Programming"

Python
print("Python" in text) Run

Output:

TruePython Run

Check if text does NOT exist

Python not in text)


print("Java" Run

Output:

TruePython Run

Important String Methods

1. lower()
Converts to lowercase.

[Link] 6/18
5/22/26, 7:17 PM Strings in Python

text = "PYTHON"

Python
print([Link]()) Run

Output:

Python
python Run

2. upper()

text = "python"

Python
print([Link]()) Run

Output:

Python
PYTHON Run

3. title()
First letter capital.

text = "python programming"

Python
print([Link]()) Run

Output:

Python
Python Programming Run

4. capitalize()
[Link] 7/18
5/22/26, 7:17 PM Strings in Python

text = "python"

Python
print([Link]()) Run

Output:

Python
Python Run

5. strip()
Removes spaces from both sides.

text = " Python "

Python
print([Link]()) Run

6. lstrip()
Left side spaces remove.

text = " hello"

Python
print([Link]()) Run

7. rstrip()
Right side spaces remove.

text = "hello "

Python
print([Link]()) Run

[Link] 8/18
5/22/26, 7:17 PM Strings in Python

8. replace()
Replace text.

text = "I like Java"

Python
print([Link]("Java", "Python")) Run

Output:

Python
I like Python Run

9. split()
Converts string into list.

text = "apple banana mango"

Python
print([Link]()) Run

Output:

Python'banana', 'mango']
['apple', Run

10. join()
Joins list into string.

items = ['Python', 'Java', 'C++']

Python
print(" - ".join(items)) Run

Output:

Python Run
[Link] 9/18
5/22/26, 7:17 PM y Strings in Python
Python - Java - C++

11. find()
Returns index of first occurrence.

text = "Python"

Python
print([Link]("t")) Run

Output:

2 Python Run

If not found:

-1 Python Run

12. count()
Counts occurrences.

text = "banana"

Python
print([Link]("a")) Run

Output:

3 Python Run

13. startswith()

[Link] 10/18
5/22/26, 7:17 PM Strings in Python
text = "Python"

Python
print([Link]("Py")) Run

Output:

TruePython Run

14. endswith()

Python
print([Link]("on")) Run

Output:

TruePython Run

15. isalpha()
Checks only letters.

text = "Python"

Python
print([Link]()) Run

Output:

TruePython Run

16. isdigit()
Checks only numbers.

[Link] 11/18
5/22/26, 7:17 PM Strings in Python
num = "12345"

Python
print([Link]()) Run

Output:

TruePython Run

17. isalnum()
Checks letters + numbers.

text = "Python123"

Python
print([Link]()) Run

Output:

TruePython Run

Escape Characters
Used with backslash \

Escape Meaning

\n New line

\t Tab

\" Double quote

\' Single quote

\\ Backslash

Example:

Python Run
[Link] 12/18
5/22/26, 7:17 PM Strings in Python
Python Run
print("Hello\nWorld")

Output:

Hello
Python
World Run

String Formatting

1. f-strings (Most Important)

name = "Vansh"
age = 17

Python name is {name} and I am {age} years old")


print(f"My Run

2. format()

name = "Vansh"

Pythonname is {}".format(name))
print("My Run

3. % Formatting (Old)

name = "Vansh"

Pythonname is %s" % name)


print("My Run

Immutability of Strings
Strings cannot be changed after creation.
[Link] 13/18
5/22/26, 7:17 PM Strings in Python

Wrong:

text = "Python"

Python
text[0] = "J" Run

Error occurs.

Correct:

text = "Python"

new_text = "J" + text[1:]

Python
print(new_text) Run

Output:

Python
Jython Run

Looping Through String

text = "Python"

for ch in text:
Python
print(ch) Run

Nested Loop Example

text = "ABC"

for ch in text:
Python
print(ch * 3) Run

Output:

[Link] 14/18
5/22/26, 7:17 PM Strings in Python

AAA
BBB
CCC Python Run

Useful Real-Life Examples

Username Checker

username = input("Enter username: ")

if [Link]():
print("Valid username")
else:
Python
print("Invalid username") Run

Palindrome Checker

text = input("Enter word: ")

if text == text[::-1]:
print("Palindrome")
else:
Python
print("Not palindrome") Run

Word Counter

sentence = input("Enter sentence: ")

words = [Link]()

Python words:", len(words))


print("Total Run

[Link] 15/18
5/22/26, 7:17 PM Strings in Python

Common Interview/String Questions


1. Reverse a string
2. Check palindrome
3. Count vowels
4. Count words
5. Remove spaces
6. Find duplicate characters
7. Check anagram
8. String compression
9. Frequency of characters
10. Convert uppercase to lowercase

Practice Questions
Beginner
1. Take a name input and print its length.
2. Print first and last character.
3. Reverse a string.
4. Count vowels in a string.
5. Check if a word is palindrome.

Intermediate
6. Count spaces in sentence.
7. Replace all spaces with _
8. Count frequency of each character.
9. Find largest word in sentence.
10. Remove duplicate characters.

Advanced
11. Check anagram.
12. String compression.
13. Find most repeated character.
14. Remove all vowels.
15. Convert sentence into camelCase.

Important Notes
[Link] 16/18
5/22/26, 7:17 PM Strings in Python

Strings are immutable.


Indexing starts from 0.
Slicing end index excluded.
Strings support loops.
Most used methods:
split()

join()
replace()

find()
strip()

lower()
upper()

ChatGPT Upgrade

Most Important Methods for Programming

lower()
Python Run
upper()
strip()
split()
join()
replace()
find()
count()
startswith()
endswith()

These are used heavily in:


Web development
AI/ML
Data science
Automation
Competitive programming

Mini Challenge For You


Write a program that:
1. Takes full name input
2. Prints name in uppercase
3. Prints total characters
4. Reverses the name

[Link] 17/18
5/22/26, 7:17 PM Strings in Python

5. Counts vowels

Example:

Python Run

Enter name: Vansh Malvi

Output:

Python Run

VANSH MALVI
Total characters: 12
Reverse: ivlaM hsnaV
Vowels: 3

[Link] 18/18

You might also like