The deeper you delve into the world of code, the clearer you must have the programming fundamentals to be able to use them correctly. In this post, we talk to you about strings in Python.
Primitive type: string or string
A chain or string is a primitive type in many programming languages, such as Python or JavaScript. Strings in Python are lists of characters that can be stored in the computer’s memory that you are using.
def: character list
greeting = “Hello, world” – Example
student1 = «Susana» – Example 2
student2 = ‘Fernando’
casa_alumno = «Susana’s home»
The previous example reflects that, in the definition of the list of characters or string, you can use quotes double or single.
Strings have several functionalities that make writing the code easier, such as accessing the characters that the strings have in the positions you want or extracting them, among others.
Below we present some uses of strings in Python.
Position of a character in a string
When using a string in a program, you can access characters in certain positions and it allows you to index the elements. To do this, use brackets:
student1 = «Susana»
student1 = [0]
> Yes
The position of the elements of the string starts counting from the number 0that is, it is always counted from a previous position.
Another use of strings in Python is the ability to create substrings from extracting characters from an original string.
student1 = «Susana»
student1 = [1:4]
> use
student1 = «Susana»
student1 = [0:6] or student1 = [:]
> Susanna
In this example, you can see how the index to extract characters 1, 2, 3, that is, uses, must be the next one to them: 4. To extract all the characters from the string, as we see in the second example, it is not necessary to put the indexes at either the beginning or the end.
Position of a character from the end: negative indices
To access the last character of the string, you must know the length of the string. Python allows you to do it without knowing it with negative indices:
casa_alumno1 = «Susana’s home»
student_house1 [-1]
> and
Thinking about indexes in programming requires imagining that strings in Python or elements like «Susana’s home» have positive indices from left to rightthat is, S = 0, U = 1, A = 3 and so on. While negative indices come from right to leftthat is, E = -1, M = -2, O = -3…
We can also output substrings with negative indices:
student2 = ‘Fernando’
student2 = [-3:-1]
> ‘nd’
Skip strings
For extract a substring that is not continuousthat is, jump between characters, you can use a double colon:
casa_alumno = «Susana’s home»
student_house = [0: :2] – The last number, 2, means that the extraction will jump in pairs.
> «Ssn’ oe»
Reverse strings
To reverse a python text string you can use the negative indices. You only have to traverse the chain from the end and thus reverse it in a negative way.
// invert a python string
casa_alumno = «Susana’s home»
student_house = [ : :-1]
> «emoh s’anasus»
Now, do you know how to invert a number mathematically in Python?…
Iterate strings
Another use of strings in Python is from its iteration. For strings, of the different types of iteration in Python, you can run for in:
student1 = «Susana»
vowels = »
for character in student1:
if character == ‘a’ or character == ‘e’ or character == ‘i’ or character == ‘o’ or character == ‘u’
vowels += character
>>>vowels
>’uaa’
Operator in
To find out if an element is a string, use the operator in.
‘u’ in student1
>True
‘u’ in student2
> False
String methods
On one or more strings in Python you can use the functions called string methods. More uses of strings in Python:
>>> student1.upper() – Upper method: Makes a copy of the string with the assigned method.
‘SUZANNE’
>>> student1.lower() – Lower method
‘Suzanne’
>>> student1.replace(«u», «x») – replace method
‘sxsana’
To know more about methods that can be implemented in stringss in Python, you can check out docs.python.
What is the next step?
Now that you are clear about the use of strings in Python, you can continue strengthening these and other tools thanks to the Introduction to Programming Course from Scratch. You will find key information on the fundamentals of programming in Python and JavaScript and other very useful resources for your process. Sign up!