[Go to site: main page, start]

0% found this document useful (0 votes)
3 views2 pages

String Operations in Python

The document provides an overview of string operations in Python, including basic operations like concatenation, repetition, and indexing, as well as common string functions such as upper(), lower(), and replace(). It also includes example code demonstrating the use of these operations and functions. Overall, it serves as a guide for manipulating strings in Python.

Uploaded by

sudeepdandagal38
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)
3 views2 pages

String Operations in Python

The document provides an overview of string operations in Python, including basic operations like concatenation, repetition, and indexing, as well as common string functions such as upper(), lower(), and replace(). It also includes example code demonstrating the use of these operations and functions. Overall, it serves as a guide for manipulating strings in Python.

Uploaded by

sudeepdandagal38
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 Operations in Python

Strings in Python are sequences of characters enclosed in quotes.


Python provides many operations and functions to work with strings.

Basic String Operations:


------------------------
1. Concatenation (+) : "Hello" + " World" = "Hello World"
2. Repetition (*) : "Hi" * 3 = "HiHiHi"
3. Length (len) : len("Python") = 6
4. Indexing : "Python"[0] = 'P'
5. Slicing : "Python"[1:4] = 'yth'
6. Membership (in, not in): "th" in "Python" = True

Common String Functions:


------------------------
1. upper() : "hello".upper() = "HELLO"
2. lower() : "HELLO".lower() = "hello"
3. capitalize() : "python".capitalize() = "Python"
4. title() : "python is good".title() = "Python Is Good"
5. strip() : " hello ".strip() = "hello"
6. replace() : "apple".replace("a", "o") = "opple"
7. find() : "hello".find("e") = 1
8. count() : "banana".count("a") = 3
9. startswith() : "python".startswith("py") = True
10. endswith() : "python".endswith("on") = True
11. split() : "a,b,c".split(",") = ['a', 'b', 'c']
12. join() : " ".join(["Hello", "World"]) = "Hello World"

Example Code:
-------------
s = " Hello Python "
print([Link]()) # Removes spaces
print([Link]()) # Converts to uppercase
print([Link]("Hello", "Hi")) # Replaces Hello with Hi
print("Pyt" in s) # Checks if substring exists

You might also like