Learn Python Series (#2) - Handling Strings Part 1

in #utopian-io6 years ago (edited)

Learn Python Series (#2) - Handling Strings Part 1

python_logo.png

What Will I Learn?

  • You will learn about creating strings,
  • about concatenating and repeating strings,
  • about indexing and slicing sequence types, including strings,
  • and about string formatting

Requirements

  • A working modern computer running macOS, Windows or Ubuntu
  • An installed Python 3(.6) distribution, such as (for example) the Anaconda Distribution
  • The ambition to learn Python programming

Difficulty

Basic / Intermediate

Curriculum (of the Learn Python Series):

Learn Python Series (#2) - Handling Strings Part 1

Handling strings is an essential part of coding in Python, as is the case with most, if not all, programming languages. Python has a number of built-in methods to handle strings, to manipulate / change them into something else, and to print and/or return them for further processing.

In this tutorial episode, we will cover creating strings, concatenating and repeating strings, the indexing and slicing of strings - which is by the way an essential ingredient to working with all sequence types -, string formatting, and in Handling Strings Part 2 I will cover a number of commonly used string functions. I hope you'll again find this Learn Python Series episode useful!

What are strings and how to create them?

Strings are a (text sequence) data type in Python, and a string consists of a number of characters which could be letters, but also numbers, spaces, punctuation such as commas and exclamation marks, or other characters. Strings are enclosed within single quotes ' or double quotes ", as long as the opening and closing quote are the same. To create a string, you just assign its value to a variable, like so:

str1 = 'I am a string enclosed in single quotes'
str2 = "And I'm a string as well"

Nota bene 1: please note how I'm in str2 contains a single quote which is part of the total string, and is not seen as the ending of the total string, because the opening quote and the closing quote are both double quotes and the quote in I'm is a single quote being a part of the total string itself. The take-away here is that quotes not only function as enclosing characters but can be a part of a string itself.

Nota bene 2: please also note that in case you would like to use single quotes as enclosing characters for str2, that that's also possible by escaping the single quote I'm by putting the \ backslash character before the single quote you want to escape, like so:

str3 = 'And I\'m a string as well'

Escaping that single quote, in this case means "This single quote is not the end of the total string, please do not interpret this single quote as the end of the string but as a part of the string".

String concatenation

Using the + operator we can "glue together" multiple strings to form a larger string, which we can then print as one string. This works with concatenating string variables as well as with concatenating string values:

part1 = "This is the f"
part2 = "irst part of th"
part3 = "e total string"

# Concatenate like this,
total = part1 + part2 + part3
print(total)
# This is the first part of the total string

# or like that,
print(part1 + part2 + part3)
# This is the first part of the total string

# or like so:
print("We could also" + " conc" + "atenate like so!")
# We could also concatenate like so!
This is the first part of the total string
This is the first part of the total string
We could also concatenate like so!

Nota bene 3: please note that string concatenation only works when all concatenated variables are of the same string data type. In case you try to concatenate (and then print) a string and an integer, you will get an error. But it is a common use case to wanting to print both text and numbers. In such a situation use explicit type conversion, meaning convert the number to a string prior to concatenating it to another string. Observe the following two examples:

# Gives error
name = 'Lionel Messi'
club = 'Barcelona'
shirt_number = 10
print(name + ' currently plays for ' + club + 
     ' wearing shirt number ' + shirt_number)
# ---------------------------------------------------------------------------
# TypeError   Traceback (most recent call last)
# <ipython-input-41-3fa7444e2234> in <module>()
#       4 
#       5 print(name + ' currently plays for ' + club + 
# ----> 6      ' wearing shirt number ' + shirt_number)
#
# TypeError: must be str, not int
# Correct concatenation, all of data type string
name = 'Lionel Messi'
club = 'Barcelona'
shirt_number = 10
print(name + ' currently plays for ' + club + 
     ' wearing shirt number ' + str(shirt_number)) # int converted to str before concatenation
Lionel Messi currently plays for Barcelona wearing shirt number 10

String repetition

Using the * operator, we can repeatedly concatenate strings, like so:

str4 = 'Blah!'
print(str4 * 3)
# Blah!Blah!Blah!

print('Repeat after me:', 'Ooh!'*5)
# Repeat after me: Ooh!Ooh!Ooh!Ooh!Ooh!
Blah!Blah!Blah!
Repeat after me: Ooh!Ooh!Ooh!Ooh!Ooh!

String size

The built-in Python function len() also works on strings, returning the string length, which means, on strings, the amount of characters contained in the string (of course including spaces).

str5 = 'To be, or not to be? That is the question'
print(len(str5))
# 41
41

Accessing substrings by index or slice

Standard Python does not include a "character" data type, however, individual characters are treated as strings with a length of 1. A (sequence of) character(s) are therefore treated as substrings. In order to access a substring inside a larger string, you can use the square brackets [] notation for slicing the string using an index number for one character or an index range (slice) for multiple characters.

Observe the following code:

str6 = "This is another string"
print(str6)      # print the total string
# This is another string

print(str6[0])   # print the first character, having index number 0
# T

print(str6[0:4]) # print the index range [0:4], including index 0, EXCLUDING index 4
# This

print(str6[:4])  # by omitting the start index, you slice from the beginning
# This

print(str6[8:])  # and by omitting the end index, you slice INCLUDING the last character
# another string
This is another string
T
This
This
another string

It is also possible to use negative index numbers for string slicing. The last character of a string can be accessed with index number -1, the one before that with index number -2, et cetera.

Nota bene 4: please note that in order to include the last character of a string using slicing, you must omit the end index number.

str6 = "This is another string"
print(str6[-1])    # print the last character of the string
# g

print(str6[-6:])   # slice the last 6 characters of the string
# string

print(str6[-6:-1]) # slice doesn't include the last character
# strin
g
string
strin

Using Strides for string slicing

Regarding string slicing, it's also possible to include an optional third slice parameter called the stride, which is like a "step" and can be negative for reversing the order of characters. Up until here we've been omitting the stride parameter, which we can, because Python defaults to a stride of 1.

On how to use the stride parameter explicitely, regard the following code:

str6 = "This is another string"
print(str6[::1])    # same as print(str6) 
# This is another string

print(str6[::2])    # print the whole string starting at index 0, 
                    # but skip every other character
# Ti saohrsrn

print(str6[::-1])   # print the whole string backward
# gnirts rehtona si sihT
This is another string
Ti saohrsrn
gnirts rehtona si sihT

Fun fact: Strides can be used for (simple) "cryptography", where a seemingly "Gibberish" string may in fact contain hidden messages! For example:

gibberish_msg = """!3*oJ6iFupOGiF6cNFSHU 6dmVhoKUrTvfHi 
                    KteBrgHvaIgsX$snTeIgmV0 HvnYGembdJRd*&i$6h&6 &5a*h BGsF@iGv NhsIgiYdh67T"""
print(gibberish_msg[::-3])
# This is a hidden message        from Scipio!
This is a hidden message        from Scipio!

Output formatting

Thus far we've only used the print() function by passing it one or more arguments. But there exist various ways to format the print output. Let's briefly review some of them.

  • C-like printf string formatting

Unlike the C language, in Python there is no printf() or sprintf() function, but Python's print() function does support the same type of behavior by using operator overloading on the modulo % operator to perform string formatting (and thus has nothing to do with the "regular" modulo functionality: to calculate numerical division remainders). Overloaded output formatting, utilizing placeholders, works like so:

Usage: %[flags][width][.precision]type

print('The number %d divided by %d equals %.2f' % (15, 2, 15/2))
# The number 15 divided by 2 equals 7.50
The number 15 divided by 2 equals 7.50

Explanation:

  • the print() function first contains a format string with 3 placeholders: %d, %d and %.2f,

  • then comes the modulo overloader %,

  • then comes a tuple (15, 2, 15/2), holding the same amount of values as there are placeholders, and in the same order,

  • the first and second placeholders %d are so-called signed integer decimals,

  • the third placeholder %.2f is a floating point decimal with precision .2 (= 2 decimal places: 7.50),

  • while printing the placeholders are replaced by their corresponding tuple values.

  • The format() string method

This is the new, (well, added first in Python 2.6), more "Pythonic" way to format strings. It functions more-or-less the same as the C-like string formatting explained above:

  • again there's a format string, also called the template, because inside it you place format codes ("placeholders"),
  • then comes the format() method in which comma-separated values are placed,
  • inside the format string (template) you place the format codes ("placeholders") which are enclosed by curly braces {},
  • the format codes function like variables to be replaced with / substituted by the field values passed to the format() method,
  • the format codes can be empty, in which case the order of values in the format() method is used, but the format codes can hold positional parameters as well, either an index number or a keyword argument, making the format string more flexible than its C-like twin string formatter,
  • and, again, the format codes can hold [width][.precision]type as well.

Examples:

print('I like {} better than {}'.format('Steem', 'SBD'))
# I like Steem better than SBD

print('I like {1} better than {0}'.format('Steem', 'SBD'))
# I like SBD better than Steem

print('I like {btc} better than {alt}'.format(alt='Tether', btc='Bitcoin'))
# I like Bitcoin better than Tether

print('The number {0} divided by {1} equals {2:.2f}'.format(15, 2, 15/2))
# The number 15 divided by 2 equals 7.50
I like Steem better than SBD
I like SBD better than Steem
I like Bitcoin better than Tether
The number 15 divided by 2 equals 7.50

What's covered in the next Handling Strings tutorial(s)?

Python's standard and string libraries have (at least) 40 more built-in methods (functions) available for manipulating strings, and we'll cover how to use (some of) them. And later on, combined with other Python language constructs, we'll cover how to combine multiple string methods to define new, self-built, functions!

Thank you for your time!



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Another great article. Can't wait for the rest. The most funny is the print(str6[::-3])

That's cool huh! Have a look at Learn Python Series (#4) - Round-Up #1 to see how to make your own "gibberish / hidden message" by encoding and decoding it!

Will do for shure. I think i will do #3 first, so i don't miss anything.

I never knew that we can learn python on steemit as well. I have interest in learning computer programming languages. At the moment feeling very sleepy so have to bookmark your post for later.

And? Did you get the chance to read & study it already? Thx for bookmarking it though! :-)

I am a newbie so trying to learn the very basics of it first. I have read it but could not get the most out of it.

I learnt some basic python years ago but never used it so it faded from my memory. Now maybe a good time to pick it back up. This looks like a great place to start!

It most certainly is! Be sure to follow along because I'm publishing a lot of Learn Python Series episodes!!

Hey @scipio I am @utopian-io. I have just upvoted you!

Achievements

  • WOW WOW WOW People loved what you did here. GREAT JOB!
  • You are generating more rewards than average for this category. Super!;)
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Thank you @utopian-io ! Bleep! Bleep!

Useful post.Thanks for sharing it

And thank you for reading it! :-) (Well, I hope you at least red it?)

Hi @scipio. I'm currently on a project of creating a local exchange of steem to my local currency but i have no idea of how to call a user's steemit info when he/she logs in into that site with their steemit account. Thought you could be the best person to help.

Hi, this tutorial episode is not about the package / module / library steem-python but about the language Python itself. There are plenty of (Utopian) tutorials out there already, look at @emrebeyler or @amosbastian 's curriculum on Steemit - among others - that provide a lot of insights on how to interact with the Steem blockchain via steem-python. I will (most probably) cover stem-python myself as well, but before that, there are a lot of other things to cover in the Learn Python Series!

I don't know what you mean exactly with creating a local exchange of steem to my local currency. As far as I know, currently, you can't even buy / sell USD for Steem/SBD directly. Steem & SBD are now being traded at exchanges (like Bittrex) where you first need to buy BTC and then buy Steem / SBD (and vice versa).

I might have misunderstood your question though! :-)

I’ll most definitely check out their pages and follow your series. And yeah you mistook my question. It’s just a site I want create to fellow Ghanaians so they can sell their steem to me and I pay them the equivalence in Ghana cedis. So I needed a way to display their steemit info on the site when they login into that site. That’s what I meant. And thanks for your reply. I appreciate it.

Much thanks to you for the commitment...

Much thanks to you for the effort

that's an educational post.
it's a learning post.
go ahead

Go ahead with what exactly? :-)

useful post ....thanks for sharing

Coin Marketplace

STEEM 0.19
TRX 0.13
JST 0.030
BTC 63574.15
ETH 3404.81
USDT 1.00
SBD 2.54