Make App Pie

Training for Developers and Artists

From Apple to Raspberry Pi: Using Strings

appleberryThere are several ordered types in Python, one of the most common is the string. In Xcode, we defined a NSString this way:

NSString *myString = @"and now for SOMEthing completely different"

Objective-C has the @"...” indicators for a string. Python on the other hand can use the much easier "..." or '...'

my_string = "and Now for something completely different"

The use of double or single quotes allows for the use of double or single quotes within the string. Python also supports Triple quotes """...""" which  allows for any white space, including new lines.

print (
""" ANd now
   for
something
           completely
    different""")
 ANd now
   for 
something 
           completely 
    different

Concatenation of strings are a  plus sign:

my_string ="and now for SOMEthing completely different"
my_silly_string ="silly walks"
print (my_string + ' -- The Ministry of ' + my_silly_string )
and now for SOMEthing completely different -- The Ministry of silly walks

There are a few functions that work on strings, such as:

string_length = len (my_string)

Methods of the str class perform string manipulations. For example there are several to deal with differing forms of capitalization:

print (my_string.capitalize())
print (my_string.upper())
print (my_string.lower())
print (my_string.title())
print (my_string.swapcase())
And now for something completely different
AND NOW FOR SOMETHING COMPLETELY DIFFERENT
and now for something completely different
And Now For Something Completely Different
AND NOW FOR someTHING COMPLETELY DIFFERENT
and now for SOMEthing completely different

and several for alignment:

my_string = "silly walks"
print (my_string.center(30))
print (my_string.center(30,'*'))
print (my_string.rjust(30,'-'))
print (my_string.ljust(30,'^'))
         silly walks          
*********silly walks**********
-------------------silly walks
silly walks^^^^^^^^^^^^^^^^^^^

and several for editing the string:

print (my_string.replace('different','penguin'))
print (my_string.partition('SOME'))
print (my_string.count('e'), " e's in our string")
print (my_string.find('e'))
and now for SOMEthing completely penguin
('and now for ', 'SOME', 'thing completely different')
4  e's in our string
27

The most used Xcode string function is for formatting numbers:

NSString *myString = [NSString stringwithFormat:@"now for a integer %i and float %6.2f",3,3.14];

In later versions of Python they replaced the % with braces and colons. The above format would be done like this:

my_string = "now for a integer {0:d} and float{1:6.2f}".format(3,3.141)

The braces tell Python to substitute something there. At the simplest  in the latest versions, you can do this:

my_string = "now for a integer {} and float{} and string {}".format(3,3.141, "oh my!")

You can also do this, which marks the position in the parameters:

my_string = "now for a integer {0} and float{1} and string {2}".format(3,3.141, "oh my!")
 

This explicitly grabs from the first, second or third parameters. like stringWithFormat:, the list of all the possibilities is quite long. I suggest looking up the documentation for all the possibilities.

I was going to do some more sequential structure next, but I wanted to do something a bit more practical. Python does not have a UI Foundation, but it does have a module which does many of the same things: tkinter. I will begin to explore that next.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: