Docsity
Docsity

Prepara i tuoi esami
Prepara i tuoi esami

Studia grazie alle numerose risorse presenti su Docsity


Ottieni i punti per scaricare
Ottieni i punti per scaricare

Guadagna punti aiutando altri studenti oppure acquistali con un piano Premium


Guide e consigli
Guide e consigli


Guida all'Uso delle Stringhe in Python, Schemi e mappe concettuali di Elementi di Informatica

Una panoramica completa sull'utilizzo delle stringhe in python, partendo dalle basi come la dichiarazione e l'inizializzazione, fino ad arrivare a concetti più avanzati come la concatenazione, la ripetizione e la conversione di tipi di dati. Vengono inoltre illustrati i metodi utili per la manipolazione delle stringhe e le sequenze di escape. Il documento include esempi pratici e spiegazioni dettagliate per facilitare la comprensione e l'applicazione delle stringhe nella programmazione python. Infine, vengono trattati input e output formattati, essenziali per interagire con l'utente e visualizzare i risultati in modo chiaro e preciso. Ideale per chiunque voglia imparare a programmare con python e necessiti di una guida completa e ben strutturata sull'uso delle stringhe.

Tipologia: Schemi e mappe concettuali

2024/2025

In vendita dal 26/07/2025

donato993
donato993 🇮🇹

27 documenti

1 / 29

Toggle sidebar

Questa pagina non è visibile nell’anteprima

Non perderti parti importanti!

bg1
PROGRAMMARE CON LE STRINGHE
Capitolo 2
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d

Anteprima parziale del testo

Scarica Guida all'Uso delle Stringhe in Python e più Schemi e mappe concettuali in PDF di Elementi di Informatica solo su Docsity!

PROGRAMMARE CON LE STRINGHE

Capitolo 2

Introduction

  • Numbers and character strings are important data types in any Python program
    • These are the fundamental building blocks we use to build more complex data structures
  • In this chapter, you will learn how to work with numbers and text. We will write several simple programs that use them

Contents 2.1 Variables 2.2 Arithmetic 2.3 Problem Solving: First Do It By Hand 2.4 Strings 2.5 Input and Output

2.3 Problem Solving

DEVELOP THE ALGORITHM FIRST, THEN WRITE THE

PYTHON

Start with example values

  • Givens
  • Total width: 100 inches
  • Tile width: 5 inches
  • Test your values
    • Let’s see… 100/5 = 20, perfect! 20 tiles. No gap.
    • But wait… BW…BW “…first and last tile shall be black.”
  • Look more carefully at the problem….
    • Start with one black, then some number of WB pairs
    • Observation: each pair is 2x width of 1 tile
      • In our example, 2 x 5 = 10 inches

Keep applying your solution

  • Total width: 100 inches
  • Tile width: 5 inches
  • Calculate total width of all tiles
  • One black tile: 5”
  • 9 pairs of BWs: 90”
  • Total tile width: 95”
  • Calculate gaps (one on each end)
  • 100 – 95 = 5” total gap
  • 5 ” gap / 2 = 2.5” at each end

The algorithm

  • Calculate the number of pairs of tiles
    • Number of pairs = integer part of (total width – tile width) / (2 * tile width)
  • Calculate the number of tiles
    • Number of tiles = 1 + (2 * number of pairs)
  • Calculate the gap
    • Gap at each end = (total width – number of tiles * tile width / 2
  • Print the number of pairs of tiles
  • Print the total number of tiles in the row
  • Print the gap

2.4 Strings

String Length

  • The number of characters in a string is called the length of the string. (For example, the length of "Harry" is 5).
  • You can compute the length of a string using Python’s len() function: length = len("World!") # length is 6
  • A string of length 0 is called the empty string. It contains no characters and is written as "" or ''.

String Concatenation (“+”)

  • You can ‘add’ one String onto the end of another firstName = "Harry" lastName = "Morgan" name = firstName + lastName # HarryMorgan print(“my name is:”, name)
  • You wanted a space in between the two names? name = firstName + " " + lastName # Harry Morgan Using “+” to concatenate strings is an example of a concept called operator overloading. The “+” operator performs different functions of variables of different types

Converting Numbers to Strings

  • Use the str() function to convert between numbers and strings.
  • Open Wing, then open a new file and type in: balance = 888. dollars = 888 balanceAsString = str(balance) dollarsAsString = str(dollars) print(balanceAsString) print(dollarsAsString)
  • To turn a string containing a number into a numerical value, we use the int() and float() functions: id = int("1729") price = float("17.29") print(id) print(price)
  • This conversion is important when the strings come from user input.

Strings and Characters

  • strings are sequences of characters
    • Python uses Unicode characters
      • Unicode defines over 100,000 characters
      • Unicode was designed to be able to encode text in essentially all written languages
    • Characters are stored as integer values
      • See the ASCII subset on Unicode chart in Appendix A
      • For example, the letter ‘H’ has a value of 72

String Operations

Methods

  • In computer programming, an object is a software entity that represents a value with certain behavior.
    • The value can be simple, such as a string, or complex, like a graphical window or data file.
  • The behavior of an object is given through its methods.
    • A method is a collection of programming instructions to carry out a specific task – similar to a function
  • But unlike a function , which is a standalone operation, a method can only be applied to an object of the type for which it was defined.
    • Methods are specific to a type of object
    • Functions are general and can accept arguments of different types
  • You can apply the upper() method to any string, like this:
    • name = "John Smith"
    • Sets uppercaseName to "JOHN SMITH"

    • uppercaseName = name.upper()