Thursday, December 7, 2017

Working with Strings in python at codelent

You’ve seen strings before, and know how to make them. You’ve also looked at how to access
their individual characters by indexing and slicing. In this post, you see how to use them to
format other values (for printing, for example), and take a quick look at the useful things you
can do with string methods, such as splitting, joining, searching, and more.
Basic String Operations
All the standard sequence operations (indexing, slicing, multiplication, membership, length,
minimum, and maximum) work with strings, as you saw in the previous chapter. Remember,
however, that strings are immutable, so all kinds of item or slice assignments are illegal:
>>> website = 'http://www.python.org'
>>> website[-3:] = 'com'
Traceback (most recent call last):
File "<pyshell#19>", line 1, in ?
website[-3:] = 'com'
TypeError: object doesn't support slice assignment

String Formatting: The Short Version
If you are new to Python programming, chances are you won’t need all the options that are
available in Python string formatting, so I’ll give you the short version here. If you are interested
in the details, take a look at the section “String Formatting: The Long Version,” which
follows. Otherwise, just read this and skip down to the section “String Methods.”
String formatting uses the (aptly named) string formatting operator, the percent (%) sign.
¦Note As you may remember, % is also used as a modulus (remainder) operator.
To the left of the %, you place a string (the format string); to the right of it, you place the
value you want to format. You can use a single value such as a string or a number, you can use
a tuple of values (if you want to format more than one), or, as I discuss in the next chapter, you
can use a dictionary. The most common case is the tuple:

>>> format = "Hello, %s. %s enough for ya?"
>>> values = ('world', 'Hot')
>>> print format % values
Hello, world. Hot enough for ya?

¦Note If you use a list or some other sequence instead of a tuple, the sequence will be interpreted as a single
value. Only tuples and dictionaries will allow you to format more than one value.
The %s parts of the format string are called conversion specifiers. They mark the places where the values are to be inserted. The s means that the values should be formatted as if they were strings; if they aren’t, they’ll be converted with str. This works with most values. For a list of other specifier types, ¦Note To actually include a percent sign in the format string, you must write %% so Python doesn’t mistake
it for the beginning of a conversion specifier.
If you are formatting real numbers (floats), you can use the f specifier type and supply the precision as a . (dot), followed by the number of decimals you want to keep. The format specifier always ends with a type character, so you must put the precision before that:
>>> format = "Pi with three decimals: %.3f"
>>> from math import pi
>>> print format % pi
Pi with three decimals: 3.142

0 comments:

Post a Comment