Concatenating Strings
You can join strings up by using the + sign. This is basically the same as concatenating lists together. In previous scripts I have used a comma in print statements where there are strings mixed with variables. This has the effect of adding a space between them - normally this is fine but there may be circumstances where you don't want a space. Concatenation gets around that. However, it does require all parts to be strings, otherwise it throws an error. See str() below.Here is an example on the Python command line.
>>> a = 'Hello'
>>> b = 'World'
>>> c = 42
>>> print (a, b, c)
Hello World 42
>>> print (a,b,c)
Hello World 42
>>> print (a+b)
HelloWorld
>>> print (a+b+c)
Traceback (most recent call last):
File "<pyshell#14>", line 1, in <module>
print (a+b+c)
TypeError: must be str, not int
>>> print(a+b+str(c))
HelloWorld42
>>>
str()
This function turns other data types back into strings. There are various reasons for doing this - one reason is if you are concatenating outputs into one big string, Python expects all the components to be strings. Another is if you want to sort a list ASCIIbetically, but there are numbers scattered in there, you can convert numbers to strings.Forgive the longwinded workaround - it's one way I could use a for loop to change in-situ the list it is working through. Anyway, the point is that the list is now full of strings. str() works for integers, floats and Boolean values, though not for lists.>>> samplelist = ['This', 'is', 'a', 'sample', 'list', 42, 3.1415]>>> print (samplelist)
['This', 'a', 'is', 'list', 'sample', 42, 3.1415]
>>> for x in samplelist:
y = str(x)
z = samplelist.index(x)
samplelist[z] = y
>>> print (samplelist)
['This', 'a', 'is', 'list', 'sample', '42', '3.1415']
>>>
Changing Case with lower(), upper() and capitalize()
This is more than just presentation - Python (and a few other computer languages and programs) are case sensitive, so changing the case will change how the string is evaluated. For example on the Python command line:>>> var = 'HEllO woRLd'
>>> print (var.lower())
hello world
>>> print (var.upper())
HELLO WORLD
>>> print (var.capitalize())
Hello world
>>>
split()
Put simply, split() is how you turn a string into a list. There should be a delimiter, a character or series of characters that tells the split() function where one element ends and another element begins - this delimiter is given as a string in the brackets. For example:>>> print('Hello world this is a random string'.split(' '))Note that the first element on the last line is an empty string, '', as the string starts with a delimiter '/'.
['Hello', 'world', 'this', 'is', 'a', 'random', 'string']
>>> linuxpath = '/home/john/Dropbox/Misc Programming/Python/python3'
>>> print (linuxpath.split('/'))
['', 'home', 'john', 'Dropbox', 'Misc Programming', 'Python', 'python3']
>>>
You might wonder why, if I generally use Windows, I didn't give an example of a Windows file path being split up. I actually had a go at that, but then realised that the delimiter in Windows is not forward slash '/' (which Python handles like any other character) but backslash '\' which is the escape character. This escape character \ tells Python to interpret a string differently from how it normally would, so Python has difficulty dealing with the unmodified string of a Windows file path. Python modules and functions that deal with directory paths have to take this into account.
You can also split() a string without any delimiter. In this case, it assumes a default of spaces.
>>> print (linuxpath.split())If you really want to split it into a list of individual characters, one character per element, you can use list() which is an built-in function:
['/home/john/Dropbox/Misc', 'Programming/Python/python3']
>>>
>>> print (list(linuxpath))
['/', 'h', 'o', 'm', 'e', '/', 'j', 'o', 'h', 'n', '/', 'D', 'r', 'o', 'p', 'b', 'o', 'x', '/', 'M', 'i', 's', 'c', ' ', 'P', 'r', 'o', 'g', 'r', 'a', 'm', 'm', 'i', 'n', 'g', '/', 'P', 'y', 't', 'h', 'o', 'n', '/', 'p', 'y', 't', 'h', 'o', 'n', '3']
>>>
Strings as Sequences
Strings can be viewed as either single entities, or a sequence of characters. And as a sequence, like lists, you can put strings into a for loop, and it will be looped over, character by character.Also, as shown previously, I can measure the length of a string with the len() function.
Characters as elements
In a similar way to picking out the elements of lists, you can look at the individual characters in a string, using the same syntax. As usual with Python, the index of the characters starts at 0 and ends at length -1.As in lists, you can tell Python to return the character at a specified index
>>> print (linuxpath)or use index() to find the first position of a character in the string
/home/john/Dropbox/Misc Programming/Python/python3
>>> print (linuxpath[7])
o
>>> print (linuxpath[6])
j
>>>
>>> print (linuxpath.index('M'))and of course use len() to find the length (in characters) of the string
19
>>>
>>> print(len(linuxpath))
50
>>>
count()
You learn something new every day, and today I've found the function count() which works for both lists and strings (and I suspect a few other data types). It works assequencename.count(searchitem)
and returns the number of times the search item turns up in the sequence or collection.
>>> print(linuxpath.count('/'))
6
>>> zlist = list(linuxpath)
>>> print (zlist.count('/'))
6
>>> print (zlist.count('z'))
0
>>>
No comments:
Post a Comment