← Back to Lessons
Edit on Github

Working with or manipulating strings with Python

What is a string?

A bunch of letters and characters all together in a particular order, the only way to store any characters that are not a number, are a fundamental part of every major program.

Strings are also part of the most primitive or basic set of data-types:

TypeExampleRepresentation
String"Hello World"strjust characters in a sequence
Number23.34int, float, complexjust numbers
Sequence[2,3,1,56,4.34]list, tuple, rangeIterable list of values with numerical indexes for positions
Set{'1,'2','45'}set, frozensetLike Sequence but unordered and with duplicate elements
Mapping{"name": "Bob"}dictLike Sequence but indexes are characters intead of incremental numbers
BooleanTrue or Falsebooljust True or False
Binary01001010111bytes, bytearray, memoryviewIdeal for low level operations

How to create a string

To create a string in python just put a bunch of characters within quotes like this "hello" or even like this "23232".

name = "Bob" age = "23" # <--- this is still a string (it's within quotes)

When coding a web application, everything the user types in forms it's considered a string, even if the user types the number 2 (two) it will still be considered the string "2" and not a real number, the developer will have to explicitely convert or parse that string into a number using the function int() or float().

🔗 How to convert strings into integers with python (3 min read).

The most common use for a string is printing it using the function print

print("Hello World!") # The function print() receives a string and displays it on the command line/terminal.

How do we use strings?

String concatenation (summing strings)

Python allows to sum together strings using the plus + operator. The following fragment demonstrates how to add two strings to create a full name from first and last names.

first_name = "Alejandro" last_name = "Sanchez" full_name = first_name + " " + last_name print("My name is "+full_name) # Output: "My name is Alejandro Sanchez"

In this example "My name is " it's being concatenated with the value on the variable full_name.

The length of the string

You often want to know what the length (size) of a string is, for example: Twitter does not allow tweets with more than 240 characters.

tweet = "Good morning!" print("The variable tweet contains "+str(len(tweet))+" characters") # Output: The variable tweet contains 13 characters

Extracting characters

After we also need to know the value of the string in a particular position, for example: If a string ends with a question mark it's probably a question:

question = "How are you?" size = len(question) print("The strings start with " + question[0]) # Output: The strings start with H print("The strings ends with " + question[size - 1]) # Output: The strings ends with ?

☝ This method of character extraction on strings is very similar to the one used on lists to extract an element from a particular position in the list.

You can also extract several characters at once. The range of the method starts with the index of the first character to be extracted and ends with the index AFTER the last character to be extracted:

name = "My name is Alejandro Sanchez" print("Extracted " + name[11:20]) # Output: Extracted Alejandro print("Extracted " + name[11:]) # Output: Extracted Alejandro Sanchez print("Extracted " + nombre[:10]) # Output: Extracted My name is

Comparing strings

If you want to compare two strings you can use the == (double equal) and it will return True if the strings are EXACTLY the same, string comparison is case sensitive, "Bob" is not equal to "bob".

name1 = "pepe"; name2 = "juan"; if name1 == name2: print("This is False, I will not get printed") if name1 == "pepe": print("This is True, I will get printed") if name1 != name2: print("This is True, I will get printed")

Converting to lower or upper case.

lowercased_string = name1.lower() # will convert to lowercase uppercased_string = name2.upper() # will convert to uppercase

☝ it is good practice to always lowercase strings before comparing them with others, that way we will avoid missing case sensitive differences.

Convert strings to numbers (and vice versa)

number = 3.4 # I am a number number_as_string = str(number) # I am a string with value "3.4"

More information about strings

If you want to learn more, we suggest you start practicing instead of reading because there is nothing much to read about strings, here is a small 3 min video explaining strings.

Keep practicing!


Subscribe for more!


COMPANY

ABOUT

CONTACT

MEDIA KIT

SOCIAL & LIVE LEARNING

The most efficient way to learn: Join a cohort with classmates like yourself, live streamings, coding jam sessions, live mentorships with real experts and keep the motivation.

INTRO TO CODING

From zero to getting paid as a developer, learn the skills of the present and future. Boost your professional career and get hired by a tech company.

DATA SCIENCE

Start a career in data science and analytics. A hands-on approach with interactive exercises, chat support, and access to mentorships.

30DAYSOFGEEKCODING

Keep your motivation with this 30 day challenge. Join hundreds of other developers coding a little every day.

A.I. & MACHINE LEARNING

Start with Python and Data Science, Machine Learning, Deep Learning and maintaining a production environment in A.I.


©4Geeks Academy LLC 2019

Privacy policies


Cookies policies


Terms & Conditions