[Go to site: main page, start]

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

String Python

The document provides an overview of string manipulation in programming, covering topics such as string creation, multiline strings, iteration, length checking, slicing, case conversion, whitespace removal, replacement, splitting, concatenation, formatting, and escape characters. It includes examples for each concept to illustrate their usage. Overall, it serves as a guide for handling strings effectively in code.
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)
14 views5 pages

String Python

The document provides an overview of string manipulation in programming, covering topics such as string creation, multiline strings, iteration, length checking, slicing, case conversion, whitespace removal, replacement, splitting, concatenation, formatting, and escape characters. It includes examples for each concept to illustrate their usage. Overall, it serves as a guide for handling strings effectively in code.
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

String

The sequence of characters enclosed within a single


quote or double quotes…

Multiline string
You can assign a multiple string to a variable by using t
hree quotes

a= (“”” “””)

Looping through strings


Even strings are iterable objects,they contain a sequenc
e of characters

For x in “banana”:
print(x)

String length
To get the length of a string,use the len() function

a=”Hello world”
print(len(a))
Checking String
To check if a certain phrase or character is present in a
string,we can use the keywords in or not in

txt=”Python is a High level language”


if(“High” in txt):
print(“yes”)
Slicing String
You can return a range of characters by using the slic
e [Link] the start index and the end index, separated b
y a colon, to return a part of the string.

A=”Hello world”
print(A[2:5])

Uppercase

A=”Hello world”
print([Link]())

Lowercase

A=”HELLO WORLD”
print([Link]())

Remove whitespace

We can remove a white space

A=” Hello world”


print([Link]())

Replace a string
The replace() method replaces a specified ph
rase with another specified phrase.

A=”Welcome to all”
print([Link](“all”,”Techvolt”))

Split string
The split() method splits a string into a list.

A=”Welcome to all”
print([Link](“,”))

String concatenation
To concatenate, or combine, two strings you c
an use the + operator.

Ex:1
A=”Welcome”
B=”to all”
c=A+” ”+b;
print(C)
Ex:2
age=21
txt=”I’m Nivetha and my age { } ”
print([Link](age))

Format String
The format() method allows you to format sele
cted parts of a string.

quantity=3
items=567
price=50
myorder=”I want to pay {2} dollars for {0} pieces of it
em {1}”
print([Link](quantity,items,price))

Escape character

To insert characters that are illegal in a string, use an


escape character.

An escape character is a backslash \ followed by the


character you want to insert.

txt=”I’m \”Nivi\” my age is \\25”


print(txt)

You might also like