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

Python String Methods

The document provides an overview of Python string methods and formatting techniques, including string operators, C-style formatting, the format method, and f-strings. It explains various operators for string manipulation, such as concatenation, repetition, and slicing, along with examples. Additionally, it highlights the advantages of f-strings for dynamic string formatting and their ability to evaluate expressions at runtime.

Uploaded by

Jangam Swathi
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)
14 views13 pages

Python String Methods

The document provides an overview of Python string methods and formatting techniques, including string operators, C-style formatting, the format method, and f-strings. It explains various operators for string manipulation, such as concatenation, repetition, and slicing, along with examples. Additionally, it highlights the advantages of f-strings for dynamic string formatting and their ability to evaluate expressions at runtime.

Uploaded by

Jangam Swathi
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
You are on page 1/ 13

Python String Methods

String Operators
a= 'Data' and b = 'Analysts'

Operator Description Example

+ Concatenation - Adds values on either side of a + b will give


the operator DataAnalysts

* Repetition - Creates new strings, concatenating b*2 will give -


multiple copies of the same string AnalystsAnalysts

[ ] Slice - Gives the character from the given index a[1] will give a
String Operators
a= 'Data' and b = 'Analysts'

Operator Description Example

[ : ] Range Slice - Gives the characters from the b[0:3] will give Ana
given range

in Membership - Returns true if a character exists ‘D’ in a will give True


in the given string

not in Membership - Returns true if a character does ‘l’ not in b will give
not exist in the given string False
String Operators
a= 'Data' and b = 'Analysts'

Operator Description Example

Raw String - Suppresses actual meaning of


Escape characters. The syntax for raw strings c = "1" + "\t" + "hello"
is exactly the same as for normal strings with d = "2" + r"\t" + "all"
r/R the exception of the raw string operator, the print(c)
letter "r," which precedes the quotation marks. print(d)
The "r" can be lowercase (r) or uppercase (R) Output- 1 hello
and must be placed immediately preceding 2\tall
the first quote mark.
Python String Formatting
Python uses C-style string formatting to create new, formatted strings using string
format operator %

Character Description

%c Character.

%s String conversion via str() prior to formatting.

%i Signed decimal integer.

%d Signed decimal integer.

%u Unsigned decimal integer.

%o Octal integer.
Python String Formatting
Python uses C-style string formatting to create new, formatted strings using string
format operator %

Character Description
%x Hexadecimal integer using lowercase letters.

%X Hexadecimal integer using uppercase letters.

%e Exponential notation with lowercase e.


%E Exponential notation with uppercase e.
%f Floating point real number.
%g The shorter of %f and %e.
%G The shorter of %f and %E.
Python String Formatting
You can use the % operator to format a string to contain text as well as values of
identifiers. Use %s where you want a value to appear. After the string, put a % operator
and mention the identifiers in parameters.

1 name = 'Data Analysts‘


print("Hello %s" % name)
Output
Hello Data Analysts

2 x=1; var='Data Analysts'print('I am


printing %s then %d' % (var,x))
Output
I am printing Data Analysts then 1
String Format method
The format method allows you to format. At the places, you want to put values, put
0,1,2,.. in curly braces. Call the format method on the string and mention the identifiers
in the parameters.

x=1; var='Data Analysts'print('I am printing {0} then


{1}'.format(var,x))

Output
I am printing Data Analysts then 1

str.format() is definitely an upgrade when compared with %- formatting introduced in


Python 2.6
F- Strings
Also called “formatted string literals,” f-strings are string literals that have an f at the
beginning and curly braces containing expressions that will be replaced with their
values. The expressions are evaluated at runtime. f-strings are faster than both %-
formatting and str.format().

x=1; var='Python for Data Science‘


print(f' I am printing {var} then {x}')

Output
I am printing Python for Data Science then 1

Note- We can also use capital F instead of f


If you use an f-string, you just need to mention the identifiers in curly braces. Also, write
‘f’ right before the string, but outside the quotes used.
F- Strings
As f-strings are evaluated at runtime, you can put any and all valid Python
expressions in them.

f'{20*2}‘
Output
40

f'{80-20 }‘
Output
60

If you use an f-string, you just need to mention the identifiers in curly braces. Also, write
‘f’ right before the string, but outside the quotes used.
Multiline F- Strings
name = 'Sachin Tendulkar' The message variable utilizes a multi-line
profession = 'cricketer'
f-string enclosed within parentheses.
country = 'India'
message = ( Each line of the f-string represents a
f'Hello {name}. ' segment of the final message,
f'You are a {profession}. ' incorporating expressions within curly
f'You are from {country}.'
braces to dynamically insert the values
)
print(message) of the corresponding variables.

Output
'Hello Sachin Tedulkar. You are a
cricketer. You are from India.'
More on F- Strings
You can use various types of quotation marks inside the expressions. You need to
make sure you are not using the same type of quotation mark on the outside of the f-
string as you are using in the expression.

f"{'Data Analysts'}" f'{"Data Analysts"}'


'Data Analysts' 'Data Analysts'
More on F- Strings
You can also use triple quotes

f"""Data Analysts""" f'''Data Analysts'''


'Data Analysts' 'Data Analysts'

If you want to use the same type of quotation mark on both the inside and the outside
of the string, then you can use \

name='Robbin'
age=50 Output
f"The \"player \" is {name}, aged
{age}." 'The "player " is Robbin, aged 50.'

You might also like