What are some characteristics strings and lists have in common?

6.2. Strings and Lists¶

Throughout the first chapters of this book we have used strings to represent words or phrases that we wanted to print out. Our definition was simple: a string is simply some characters inside quotes. In this chapter we explore strings in much more detail.

Additionally, we explore lists, which are very much like strings but can hold different types.

Strings and Character Data in Python

by John Sturtz basics python
Mark as Completed
Tweet Share Email

Table of Contents

Remove ads

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Strings and Character Data in Python

In the tutorial on Basic Data Types in Python, you learned how to define strings: objects that contain sequences of character data. Processing character data is integral to programming. It is a rare application that doesn’t need to manipulate strings at least to some extent.

Here’s what you’ll learn in this tutorial: Python provides a rich set of operators, functions, and methods for working with strings. When you are finished with this tutorial, you will know how to access and extract portions of strings, and also be familiar with the methods that are available to manipulate and modify string data.

You will also be introduced to two other Python objects used to represent raw byte data, the bytes and bytearray types.

Take the Quiz: Test your knowledge with our interactive “Python Strings and Character Data” quiz. Upon completion you will receive a score so you can track your learning progress over time:

Take the Quiz »

Lists and Tuples in Python

by John Sturtz basics python
Mark as Completed
Tweet Share Email

Table of Contents

Remove ads

Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Lists and Tuples in Python

Lists and tuples are arguably Python’s most versatile, useful data types. You will find them in virtually every nontrivial Python program.

Here’s what you’ll learn in this tutorial: You’ll cover the important characteristics of lists and tuples. You’ll learn how to define them and how to manipulate them. When you’re finished, you should have a good feel for when and how to use these object types in a Python program.

Take the Quiz: Test your knowledge with our interactive “Python Lists and Tuples” quiz. Upon completion you will receive a score so you can track your learning progress over time:

Take the Quiz »

Python | Set 3 [Strings, Lists, Tuples, Iterations]

In the previous article, we read about the basics of Python. Now, we continue with some more python concepts.

Strings in Python
A string is a sequence of characters. It can be declared in python by using double-quotes. Strings are immutable, i.e., they cannot be changed.

Python




# Assigning string to a variable
a = "This is a string"
print [a]

Lists in Python
Lists are one of the most powerful tools in python. They are just like the arrays declared in other languages. But the most powerful thing is that list need not be always homogeneous. A single list can contain strings, integers, as well as objects. Lists can also be used for implementing stacks and queues. Lists are mutable, i.e., they can be altered once declared.

Python




# Declaring a list
L = [1, "a" , "string" , 1+2]
print L
L.append[6]
print L
L.pop[]
print L
print L[1]

The output is :

[1, 'a', 'string', 3] [1, 'a', 'string', 3, 6] [1, 'a', 'string', 3] a

Tuples in Python
A tuple is a sequence of immutable Python objects. Tuples are just like lists with the exception that tuples cannot be changed once declared. Tuples are usually faster than lists.



Python




tup = [1, "a", "string", 1+2]
print[tup]
print[tup[1]]

The output is :

[1, 'a', 'string', 3] a

Iterations in Python
Iterations or looping can be performed in python by ‘for’ and ‘while’ loops. Apart from iterating upon a particular condition, we can also iterate on strings, lists, and tuples.
Example 1: Iteration by while loop for a condition

Python




i = 1
while [i < 10]:
print[i]
i += 1

The output is :

1 2 3 4 5 6 7 8 9

Example 2: Iteration by for loop on string

Python




s = "Hello World"
for i in s :
print [i]

The output is :

H e l l o W o r l d

Example 3: Iteration by for loop on list

Python




L = [1, 4, 5, 7, 8, 9]
for i in L:
print [i]

The output is :

1 4 5 7 8 9

Example 4 : Iteration by for loop for range

Python




for i in range[0, 10]:
print [i]

The output is :

0 1 2 3 4 5 6 7 8 9

  • Next Article – Python: Dictionary and Keywords
  • Quizon Data Types in Python




Article Tags :
Python
School Programming
python-list
python-tuple
Practice Tags :
python-list
Read Full Article

Video liên quan

Bài mới nhất

Chủ Đề