Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
19 views

Strings Python

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Strings Python

Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Strings are Arrays

Like many other popular programming languages, strings in Python are arrays of
bytes representing unicode characters.

However, Python does not have a character data type, a single character is simply a
string with a length of 1.

Square brackets can be used to access elements of the string.

Looping Through a String


Since strings are arrays, we can loop through the characters in a string, with a
for loop.

String Length
To get the length of a string, use the len() function.
a = "Hello, World!"
print(len(a))

Check String
To check if a certain phrase or character is present in a string, we can use the
keyword in.
txt = "The best things in life are free!"
print("free" in txt)

Check if NOT
To check if a certain phrase or character is NOT present in a string, we can use
the keyword not in.

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


print("expensive" not in txt)

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


if "expensive" not in txt:
print("No, 'expensive' is NOT present.")

Python - Slicing Strings

Slicing

You can return a range of characters by using the slice syntax.

Specify the start index and the end index, separated by a colon, to return a part
of the string.

MKV= "NARUTO UZUMAKI"


print(MKV[0:14])
run:NARUTO UZUMAKI
SLICE FROM START:
no need to keep o in 0:14 ife want to start from start.

SLICE FROM LAST:


there is nooneed to keep 14 if we want to print total

Negative Indexing
Use negative indexes to start the slice from the end of the string:

the negative indicates from last


ex
MKV= "NARUTO UZUMAKI"
print(MKV[-14:-8])
run:NARUTO
Python - Modify Strings
Upper Case:
The upper() method returns the string in upper case:
ex:
MKV= "naruto uzumaki"
print(MKV.upper())
run:NARUTO UZUMAKI
Lower Case:
The lower() method returns the string in lower case:
ex:
MKV= "NARUTO UZUMAKI"
print(MKV.lower())
run:naruto uzumaki
Remove Whitespace
Whitespace is the space before and/or after the actual text, and very often you
want to remove this space.
The strip() method removes any whitespace from the beginning or the end:
ex:
MKV= " NARUTO UZUMAKI "
print(MKV.strip())

String Concatenation
To concatenate, or combine, two strings you can use the + operator.
Merge variable a with variable b into variable c:

a = "Hello"
b = "World"
c = a + b
print(c)
To add a space between them, add a " ":
String Format
As we learned in the Python Variables chapter, we cannot combine strings and
numbers like this:

age = "36"
txt = "My name is John, I am " + age
print(txt)

But we can combine strings and numbers by using f-strings or the


format() method!

F-Strings
F-String was introduced in Python 3.6, and is now the preferred way of formatting
strings.

To specify a string as an f-string, simply put an f in front of the string literal,


and add curly brackets {} as placeholders for variables and other operations.
age = 36
txt = f"My name is John, I am {age}"
print(txt)
Placeholders and Modifiers
A placeholder can contain variables, operations, functions, and modifiers to format
the value.

price = 59
txt = f"The price is {price} dollars"
print(txt)

age=17
dob="04-11-2006"

txt =f"my name is kartheek my age is { age} my DOB is {dob}"


print(txt)

A placeholder can include a modifier to format the value.

A modifier is included by adding a colon : followed by a legal formatting type,


like .2f which means fixed point number with 2 decimals:

Example
Display the price with 2 decimals:

price = 59
txt = f"The price is {price:.2f} dollars"
print(txt)
Perform a math operation in the placeholder, and return the result:
txt = f"The price is {20 * 59} dollars"
print(txt)

You might also like