Python language basics 15: string basics part 2

Introduction

In the previous post we saw how strings are represented and constructed in Python. We also mentioned other important terms such as string concatenation, immutability and character escaping.

In this post we’ll look at a couple of other important things regarding strings. Bear in mind that Python offers a very large library regarding strings. It could take the rest of the year to blog about all of it. Here we’ll only discuss a handful. Here are a couple of useful links to help you further:

The string constructor

We saw an example of the string constructor str() in this post. Here’s a reminder of the code:

print('Congratulations. It took you ' + str(guessCounter) + ' guesses to guess the correct number.')

guessCounter was an integer and it had to be converted into a string to fit the string concatenation. You can pass in any argument to the str() string constructor and you’ll get the string representation of that argument:

myIntegerString = str(132)
myBooleanString = str(True)

myIntegerString will evaluate to “132” and myBooleanString to “True”.

Extracting individual characters

We haven’t talked about arrays and collections yet but strings are in effect collections of characters. The string “hello” is made up of 5 characters. What if you want to check the value of the 3rd character in a string? It’s easy using the square-bracket operator []. You can pass in an integer to those brackets which denotes the position of the character you’d like to extract as follows:

hello = "hello"
first = hello[1]

That’s how you extract the first character of a string, right? If you’re new to programming then it seems logical that 1 should denote the first element. However, collections are 0 based in Python and probably all remotely popular programming languages out there. The variable “first” will actually get the value “e”. So if you truly want to extract the first character then you’ll need to pass in 0 as the enumerator:

hello = "hello"
first = hello[0]

String functions

There’s a myriad of built-in functions available for string types in Python. The above links will provide you with some good examples. Let’s just look at 2 of them here.

Say you’d like to capitalise all characters of a string, i.e. convert “hello” to “HELLO”. Easy, the function called “upper” helps:

hello = "hello"
helloCap = hello.upper()

Note how the function was called using the dot ‘.’ notation: string.function. That is the norm in object oriented languages. The object function is called using the dot.

The startswith method will return a boolean, i.e. True or False, indicating whether the string starts with the character that was passed in:

hello = "hello"
helloStartsWithH = hello.startswith("h")

helloStartsWithH will be True. However, the following will yield false:

helloStartsWithH = hello.startswith("H")

This means that this is a case-sensitive comparison. In general strings are case-sensitive in most programming languages, i.e. “hello” is not the same as “Hello” or “helLo”.

We’ll look at arrays in the next post.

Read all Python-related posts on this blog here.

Advertisements

About Andras Nemes
I'm a .NET/Java developer living and working in Stockholm, Sweden.

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 )

Google+ photo

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

Connecting to %s

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

Bite-size insight on Cyber Security for the not too technical.

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: