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

String Manipulation

The document provides a guide on string manipulation in Python, covering how to check for the presence of words, slice strings, modify strings (including changing case, removing whitespace, replacing substrings), and split strings into lists. It also explains string concatenation and introduces basic Python operators like modulus, exponentiation, and floor division. Overall, it serves as a comprehensive overview of string handling techniques in Python programming.

Uploaded by

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

String Manipulation

The document provides a guide on string manipulation in Python, covering how to check for the presence of words, slice strings, modify strings (including changing case, removing whitespace, replacing substrings), and split strings into lists. It also explains string concatenation and introduces basic Python operators like modulus, exponentiation, and floor division. Overall, it serves as a comprehensive overview of string handling techniques in Python programming.

Uploaded by

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

TO CHECK IF A WORD IS IN A STRING:

txt = "The best things in life are free!"


if "free" in txt:
print("Yes, 'free' is present.")
TO CHECK IF A WORD IS NOT IN A STRING:
txt = "The best things in life are free!"
if "expensive" not in txt:
print("No, 'expensive' is NOT present.")
SLICING STRINGS:
word = "Hello, World!"
print(word[0:4])
#This will print Hell not Hello as it prints , including
lower bound till upper bound -1
Slicing from the start:
b = "Hello, World!"
print(b[:5]) # Hello
Slicing to the end:
b = "Hello, World!"
print(b[2:]) # llo, World!
Negative Indexing:
b = "Hello, World!"
print(b[-5:-2]) # this will print orl
(counts from end with 1 2 3 4 5 o then from
that to 1 2 d so including o till excluding
d )
MODIFY STRINGS:

Upper Case:
a = "Hello, World!"
print([Link]())

Lower Case:
a = "Hello, World!"
print([Link]())

Remove Whitespace:

Whitespace is the space before and/or after the


actual text, and very often you want to remove this
space.
Example
The strip() method removes any whitespace
from the beginning or the end:
a = " Hello, World! "
print([Link]()) # returns "Hello, World!"
Replace String:
Example
The replace() method replaces a string with
another string:
a = "Hello, World!"
print([Link]("H", "J")) #prints Jello,
World

Split String:
The split() method returns a list where the text
between the specified separator becomes the list
items.
Example
The split() method splits the string into
substrings if it finds instances of the separator:
a = "Hello, World!"
print([Link](",")) # returns ['Hello', '
World!'] # this basically splits the word
into parts(can be more than 2), it will
make a list of separated words before the
letter and after the letter for example a =
“Ubaid ullah khan”
print([Link](“ ”)) this will output a
list
[‘Ubaid’,‘ullah’,‘khan’] so output words
before and after a space so basically b/w
spaces
a = "Ubaid ulah khlan"
print([Link]("l"))
# this prints ['Ubaid u', 'ah kh', 'an']

String Concatenation:
To concatenate, or combine, two strings you can
use the + operator.
Example
Merge variable a with variable b into variable c:
a = "Hello"
b = "World"
c = a + b
print(c)
# ---this is the end of the string topic---
#
Python Operators:

% Modulus (MOD) x%y

** Exponentiation x ** y

// Floor Division (DIV) x // y

. If you input without a datatype listed It will


consider it a String datatype so always remember
to put it

You might also like