liv
Uploaded by
4 SLIDES
237 VUES
50LIKES

Exploring String Creation in Python: Methods and Immutability

DESCRIPTION

Discover the different ways to create strings in Python, ranging from simple hardcoding to dynamic input methods. Understand the concept of string immutability, where individual characters cannot be altered after creation. Learn how to convert other data types into strings using typecasting and explore how user inputs and file reading also yield strings. Additionally, find out how to construct a string iteratively by concatenating characters. Mastering these techniques is essential for effective programming in Python.

1 / 4

Télécharger la présentation

Exploring String Creation in Python: Methods and Immutability

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript

Playing audio...

  1. Strings in Python Creating a string

  2. Creating a string • There are many different ways to create a string • The simplest way is to hard code it into your program mystring = “Hello” • Remember individual characters of mystring cannot be changed afterward (strings are immutable) • Many of the string methods return new strings newstring = mystring.upper() • The typecast str will create a string from another data type mynumstring = str(num)

  3. Getting a string from input • When you read from the keyboard, the input function will always return a string myname = input(“What’s your name? “) • When you learn about external files, one thing you do is read from them (see Chapter 10) • The input from a file always comes in as a string or list of strings

  4. Other ways to create a string • You can build up a string one character at a time • The pattern is very similar to an accumulator • You initialize a variable to an empty string new_one = “” • Then in a loop you concatenate the new character onto the variable new_one = new_one + ch

More Related