by Dinesh Thakur Category: Python

Python can manipulate string, which can be expressed in several ways. String literals can be enclosed in matching single quotes(') or double quotes("); e.g. 'hello', "hello" etc. They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triplequoted strings), e.g.'' 'hello''', """hello""". A string is enclosed in double quotes if the string contains a single quote (and no double quotes), else it is enclosed in single quotes.

String type variables store strings as names and texts in general. We call string a sequence of symbols such as letters, numbers, punctuation marks, special characters and spaces. For example: "I see my cat" in this example every sentence is a sequence of letters (I, ,s,e,e, ,m,y, ,c,a,t). We can imagine a string as a sequence of blocks, where each letter, number or blank space occupies a position as shown.














To use the text of your program and the contents of a string, we use double quotes (") or single (') to delimit the beginning and end of the string.

Assigning a string to a variable of type string (% s):

# String type used mask %s
phrase = "I see my cat"
print ("Quote: % s" %phrase)

Another feature of strings is able to access its contents character by character. Knowing that a string has a certain size, you can access their characters using an integer to represent their position. This number is called index, and started counting at zero. This means that the first character of the string will always be in the position or index (zero).



























To access the characters of a string, we must inform the index or position of the character in square brackets ([]). As the first character of a string is index 0, we can access values from 0 to the length of the string minus 1. As in the above example, the string contains 12 characters and can access the position 0..11.

name = "Dinesh Thakur"
print (name [0]) # Position at index 0

If you try to access an index higher than the number of characters in the string, Python interpreter will issue an error message.

Python Concatenation String

Concatenation is nothing that can join two or more strings into new larger string or sometimes smaller minor cases. For example, when we have a name of a very large person, and our layout fits fewer characters than the name contain.

The content of the string type variables can be added, or rather concatenated. To concatenate two strings, use the plus (+) operator. Thus, "abc" + "d" is equal to "abcd". A special concatenation case is the repetition of a string several times. For this, we use the multiplication operator (*), "D" * 5 equals "DDDDD".

char = "abc"
# Concatenated the character, together presenting the variable char
print (char + "c")
# Concatenated the character D, together presenting the variable char
print (char + "D" * 4)
# Concatenated the character Z, and 10 (-) with the character X
print ("Z" + "-" + 10 * "X")
# Concatenated the variable (char), (z4 =), 4 times (char)
print (char + "z4 =" + char * 4 )

Python Slicing String

Slicing in Python is very important. Imagine our string "I see my cat", who was working in class 4. We can slice it in order to write only the first word, second, third or even want to use only the first three letters of the string by accessing the indices [0 : 5], this is the string slicing. The number that is left of the points is indicates the start position, and the right end of the slice.

phrase = "I see my cat"
print (phrase [0: 5])

However, care should be taken at the end, as in the previous example we use the 5, my which is in position 6 was not included. This is because the end of slicing is not included, being left out.

char = "abcdefgh"

You can also omit the number left or right to represent the beginning or the end. So [: 3] indicates the beginning to the third character (without including it), and [2:] indicates the position of the second character to the end of the string, as shown above. Note that in this case not need to know how many characters the strings have.

char = "abcdefgh"

If we omit the beginning and end of slicing, we are simply making a copy of all the characters in the string, as shown above.

We can also use negative values to indicate positions from the right. So in the last character -1, -2, the penultimate, and so on. Follow the examples below:

char = "abcdef"
print (char [: 2])
print (char [1:])
print (char [0: -2])
print (char [:])
print (char [-1:])
print (char [-2: -1])
print (char [-3: -1])


This content is very important when you have to assemble reports, presentation layouts and you do not have physical space available to display the information stored.  

String constants

Some constants defined in string module are as follows:




It returns string containing lowercase letters 'abcdefghijklmnopqrstuvwxyz '.

>>> import string

>>> string.ascii_lowercase





It return string containing uppercase letters 'ABCDEFGHIJKLMNOPQRSTUVWXYZ '.

>>> string.ascii_uppercase





It returns string containing concatenation of the ascii_lowercase and ascii_uppercase constants.

>>> string.ascii_letters





It returns the string containing digits '0123456789 '.

>>> string.digits





It returns the string containing hexadecimal characters '0123456789abcdefABCDEF'.

>>> string.hexdigits





It returns the string containing octal characters ' 01234567 '.

>>> string.octdigits





It returns the string of ASCII characters which are considered punctuation characters.

>>> string.punctuation

I!"#$%&\'()*+,-./:;<=>?@[\\]"_' {I}-'




It returns the string containing all characters that are considered whitespace like space, tab, vertical tab etc.

>>> string.whitespace

'\t\n\x0b\x0c\r '




It returns the string of characters which are considered printable. This is a combination of digits, letters, punctuation, and whitespace.

>>> string.printable


\' ()*+,-./:;<=>?@[\\]"_' {I}- \t\n\r\x0b\x0c'


About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.