0% found this document useful (0 votes)
2 views16 pages

Python - Strings

Python note for Strings Concepts

Uploaded by

katporiyahardik
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)
2 views16 pages

Python - Strings

Python note for Strings Concepts

Uploaded by

katporiyahardik
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

Python-Basics

Strings-Text Processing
Guru
Agenda

• Escape Sequences
• Raw Strings
• Doc Strings
• More formatting
• Conversions
• String Methods
• The string module

Page 1
Escapes and Special Characters

• Certain special characters have special values


• They do not have a corresponding print character
• Instead they have an effect
• For example, newline, carriage return
• To insert a special character, you “escape” it
• Using the backslash

Page 2
Escape Characters

• Newline ('\n') • Backspace ('\b')


• Carriage return ('\r') • Tab ('\t')
• Single quote ('\'') • Vertical tab ('\v')
• Double quote ("\"") • Backslash ('\\')

Page 3
Raw Strings

• The raw_input( ) function reads a line from


standard input as a raw string
• Used in interactive console programs

Page 4
Quoting

• Can use either single or double


• This makes embedded quotes easy
• Can also “escape” characters with \
• Triple quotes define “long strings”
• String that can span multiple lines
• Often used in documentation and web pages

Page 5
str( ) vs. repr( )

• str( ) converts an object to a string


• It renders a human-readable string
• print uses str( ) (and expands escapes)
• repr( ) renders a string that can be evaluated by the
interpreter
• As if typed at the >>> prompt
• So it executes correctly
• Also can use backquotes
• The interpreter uses repr( ) to echo variables
• Differences show up in numbers and escape
sequences

Page 6
str( ) vs. repr( )
Examples

>>> x = .1 >>> s = "Hello\nthere"


>>> x >>> s
0.10000000000000001 'Hello\nthere'
>>> str(x) >>> print s
'0.1‘ Hello
>>> repr(x) there
'0.10000000000000001‘ >>> repr(s)
>>> `x` "'Hello\\nthere'"
'0.10000000000000001‘

Page 7
Question

• What is the result of executing:

str(2+3)

Page 8
Format Descriptors

• %s (string, via str( ))


• %r (string, via repr( ))
• %c (character)
• %d (decimal integer)
• %x (hex integer)
• %X (uppercase hex)
• %e (scientific notation for reals)
• %f (fixed-point decimal for reals)
• %g (attempts %f; bails to %e)

Page 9
String Methods

• A lot!
• Called as <string-var>.<method>(…)
• For example, [Link]('a')
• capitalize, center, count, endswith, find,
index, isalpha, isdigit (etc., as in C), istitle,
join, lower, replace, split, strip, swapcase,
title, translate, upper, zfill

Page 10
Using split( )

• Splits a string into substrings


• Uses whitespace as a separator(default)
• You can provide your own separator
• It uses the whole separator string
• See next slide
• The re module has a better version of split

Page 11
Using split( ) and join( )
Example

>>> s = 'a,b,c d,e‘


>>> [Link]()
['a,b,c', 'd,e']
>>> [Link](',')
['a', 'b', 'c d', 'e']
>>> ','.join(['x','y','z'])
'x,y,z‘
>>> ''.join(['x','y','z'])
'xyz'

Page 12
Exercise

• Print the words from a sentence in the


reverse order of the appearance,
• Write a module that reads a string from the
console and tests to see if it is a palindrome
(spelled the same backward as forward)
(Hint: for both of these, the reverse( ) list method comes in
handy)

Page 13
Questions

Page 14
Imagination Action Joy
Page 15

You might also like