Strings
Because most programs define and gather some sort of data, and then do something useful with it, it helps to classify different types of data. The first data type we’ll look at is the string. Strings are quite simple at first glance, but you can use them in many different ways.
"what is a string in python"
"This is a string."
'This is also a string.'
This flexibility allows you to use quotes and apostrophes within your strings.
'I told my friend, "Python is my favorite language!"' "The language 'Python' is named after Monty Python, not the snake." "One of Python's strengths is its diverse and supportive community."
Changing
Case in a String with Methods
One of the simplest tasks you can do with strings changes the case of the words in a string. Look at the following code, and try to determine what’s happening:
· name = "ADA Lovelace"
· print(name.title())
In this example, the variable name refers to the lowercase string "ADA Lovelace". The method title() appears after the variable in the print() call. A method is an action that Python can perform on a piece of data. The dot (.) after the name in the name. title() tells Python to make the title() method act on the variable name. Every method is followed by a set of parentheses because methods often need additional information to do their work. That informa[1]tion is provided inside the parentheses.
The title() function doesn’t need any additional information, so its parentheses are empty. The title() method changes each word to a title case, where each word begins with a capital letter. This is useful because you’ll often want to think of a name as a piece of information. For example, you might want your pro[1]gram to recognize the input values Ada, ADA, and ADA as the same name, and display all of them as Ada.
Several other useful methods are available for dealing with the cases as well. For example, you can change a string to all uppercase or all lowercase letters like this.
· name = "Ada Lovelace"
· print(name.upper())
· print(name.lower())
· ADA LOVELACE
· Ada Lovelace
The lower() method is particularly useful for storing data. Many times you won’t want to trust the capitalization that your users provide, so you’ll convert strings to lowercase before storing them. Then when you want to display the information, you’ll use the case that makes the most sense for each string.
Using Variables
in Strings In some situations, you’ll want to use a variable’s value inside a string. For example, you might want two variables to represent a first name and the last name respectively, and then want to combine those values to display some[1]one’s full name:
· first_name = "ada"
· last_name = "Lovelace"
· full_name = f"{first_name} {last_name}"u
· print(full_name)
To insert a variable’s value into
a string, place the letter f immediately. Put braces around the name or names before the opening
quotation mark of any variable you want
to use inside the string. Python will replace each variable with its value when
the string is displayed. These strings are called f-strings. The f is for the format because Python formats the string by replacing the name of any variable
in braces with its value. The output from the previous code is:

0 Comments