Archive

Posts Tagged ‘Strings’

Python: Strings

April 6, 2009 Leave a comment

Hello everyone. I am back with Python again. Let’s try to learn it from the scratch. After reading this post, you will be understood about Strings in Python.

Strings are the basic unit of text in Python. Unlike some other programming languages, a single letter is represented as a one-letter string. Let’s try to understand it using some example code:code1

How It Works:

If you use different quotes, they may look different to you; to the Python interpreter; however all of them can be used in the same situations and are very similar.

Three different types of quotes are used in Python. First, there are the single and double quotes.

Python has one more special way of constructing strings, one that will almost always avoid the entire issue of requiring an escape character and will let you put in new lines as well: the triple quote. If you ever use a string enclosed in three quotes in a row—either single or double quotes, but all three have to be the same kind—then you do not have to worry about escaping any single instance of a single or double quote. Until Python sees three of the same quotes in a row, it won’t consider the string ended, and it can save you the need to use escape characters in some situations. See the 3rd line of code in the above figure.

Putting More than One Strings together:

To put more than one string together you can use ‘+’ operator. Suppose you want to put the two strings “Hello” and “Everyone” together. So use the following code:–

code21

Another way to specify strings is to use a format specifier. It works by putting in a special sequence of characters that Python will interpret as a placeholder for a value that will be provided by you.

code3That %s is the format specifier for a string. Several other specifiers are there. Each specifier acts as a placeholder for that type in the string; and after the string, the % sign outside of the string indicates that after it, all of the values to be inserted into the format specifier will be presented there to be used in the string.

Displaying Strings with Print:

For displaying text, a special feature is built into useful languages, one that helps the programmer display information to users. The basic way to do this in Python is by using the print function:

code4print is a function—a special name that you can put in your programs that will perform one or more tasks behind the scenes. Normally, you don’t have to worry about how it happens. In this case, the print function is an example of a built-in function, which is a function included as a part of Python, as opposed to a function that you or another programmer has written. The print function performs output—that is, it presents something to the user using a mechanism that they can see, such as a terminal, a window, a printer, or perhaps another device (such as a scrolling LED display).

In the next post I will discuss about the Numbers and Operators in Python.
Advertisements