Mming/methods/string Ings - HTM: Quotes
Mming/methods/string Ings - HTM: Quotes
https://www.programiz.com/python-progra
mming/methods/string
www.tutorialspoint.com/python/python_str
ings.htm
realpython.com/python-strings
• Example 1:
• var1 = 'Hello World!'
• var2 = "Python Programming"
• print "var1[0]: ", var1[0]
• print "var2[1:5]: ", var2[1:5]
• The output of the segment is
• var1[0]: H
• var2[1:5]: ytho
Updating Strings
• You can "update" an existing string by (re)assigning a
variable to another string. The new value can be
related to its previous value or to a completely
different string altogether. For example –
• Example 2:
• var1 = 'Hello World!'
• print "Updated String :- ", var1[:6] + 'Python'
•
• When the above code is executed, it produces the
following result –
• Updated String :- Hello Python
Escape Characters
• Following table is a list of escape or non-printable characters
that can be represented with backslash notation.
• An escape character gets interpreted; in a single quoted as
well as double quoted strings.
• Backslash notation Description
• \n Newline
• \r Carriage return
• \t Tab
Example
• print “hello1 Backslash notation"
• print "hello2 \n hello2 Newline"
• print "hello3 \r hello3 Carriage return"
• print "hello4 \t hello4 Tab"
String Special Operators
• Assume string variable a holds 'Hello' and variable b holds 'Python', then −
Operator Description Example
• + Concatenation - Adds values on either side
of the operator a + b will give HelloPython
• * Repetition - Creates new strings, concatenating
multiple copies of the same string a*2 will give –HelloHello
• [] Slice - Gives the character from the given index a[1] will give e
• [ : ] Range Slice - Gives the characters from the
given range a[1:4] will give ell
• In Membership - Returns true if a character exists
in the given string H in a will give 1
• % Format - Performs String formatting See at next section
String Formatting Operator
• One of Python's coolest features is the string format operator %.
This operator is unique to strings and makes up for the pack of
having functions from C's printf() family. Following is a simple
example –
• Example 3:
• print "My name is %s and weight is %d kg!" % ('Zara', 21)
• When the above code is executed, it produces the following result –
• My name is Zara and weight is 21 kg!
• Here is the list of complete set of symbols which can be used along
with % −
• Format Symbol Conversion
• %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
• %x hexadecimal integer (lowercase letters)
• %X hexadecimal integer (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
Other supported symbols and functionality are listed in the
following table −
Symbol Functionality
* argument specifies width or precision
- left justification
+ display the sign
<sp> leave a blank space before a positive number
# add the octal leading zero ( '0' ) or
hexadecimal leading '0x' or '0X', depending
on whether 'x' or 'X' were used.
0 pad from left with zeros (instead of spaces)
% '%%' leaves you with a single literal '%'
(var) mapping variable (dictionary arguments)
m.n. m is the minimum total width and n is the
number of digits to display after the decimal
point (if appl.)
Triple Quotes
• Python's triple quotes comes to the rescue by allowing strings
to span multiple lines, including verbatim NEWLINEs, TABs,
and any other special characters.
• The syntax for triple quotes consists of three
consecutive single or doublequotes.
• Example 4:
para_str = """this is a long string that is made up of several
lines and non-printable characters such as TAB (\t) and they
will show up that way when displayed. NEWLINEs within the
string, whether explicitly given like this within the brackets
[\n], or just a NEWLINE within the variable assignment will
also show up."""
• print (para_str)
Output of the program
this is a long string that is made up of several lines and non-
printable characters such as TAB ( ) and they will show up that
way when displayed. NEWLINEs within the string, whether
explicitly given like this within the brackets [
], or just a NEWLINE withinthe variable assignment will also show
up.
• Raw strings do not treat the backslash as a special character at all.
Every character you put into a raw string stays the way you wrote it
• print 'C:\\nowhere'
• C:\nowhere
• print r'C:\\nowhere‘
• C:\\nowhere
Unicode String
• Normal strings in Python are stored internally as 8-bit
ASCII, while Unicode strings are stored as 16-bit Unicode.
• This allows for a more varied set of characters, including
special characters from most languages in the world.
• I'll restrict my treatment of Unicode strings to the
following −
• print u'Hello, world!'
• When the above code is executed, it produces the
following result −
• Hello, world!
• As you can see, Unicode strings use the prefix u, just as
raw strings use the prefix r.
• Built-in String Methods
Python includes the following built-in methods to manipulate strings
• Python String capitalize() Method
It returns a copy of the string with only its first character capitalized.
Syntax
str.capitalize()
• Example:
str = "this is string example....wow!!!";
print "str.capitalize() : ", str.capitalize()
• Result:
• str.capitalize() : This is string example....wow!!!
• Python String lower() Method
Syntax:
• str.lower()
• Example:
• Str = "THIS IS STRING EXAMPLE....WOW!!!"; print str.lower()
• Result:
• this is string example....wow!!!
• Python String upper() Method
• The method upper() returns a copy of the string in which all case-based characters have
been uppercased.
• Syntax:
str.upper()
• Example:
str = "this is string example....wow!!!";
print "str.capitalize() : ", str.upper()
Result:
str.capitalize() : THIS IS STRIN EXAMPLE....WOW!!!