Longitud de Cadena
Сalculating the number of characters in a string is a common operation. It helps in tasks like validation, formatting, or data analysis. Python provides a built-in function len() to get the length of a string.
len(string)
The string parameter refers to the text whose length you want to determine; the len() function returns the total number of characters in that text, including spaces and punctuation marks.
12345string = "Practice is the key to success" length = len(string) print("Here is the string:", string) print("And its length is", length)
The len() function counts every character in the string, including spaces. In the example, "Practice is the key to success" has 30 characters, so len(text) returns 30.
Swipe to start coding
Time to practice:
- Get the last character of the variable
stringusing negative indexation and assign the result to the variablesymbol1. - Extract the index of the last character of the variable
stringusing thelen()method and assign the result to the variablesymbol2.
Note that len(word) equals the length of the word, but len(word)-1 equals the index of the last character of the word.
Solución
¡Gracias por tus comentarios!
single
Pregunte a AI
Pregunte a AI
Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla
Genial!
Completion tasa mejorada a 3.45
Longitud de Cadena
Desliza para mostrar el menú
Сalculating the number of characters in a string is a common operation. It helps in tasks like validation, formatting, or data analysis. Python provides a built-in function len() to get the length of a string.
len(string)
The string parameter refers to the text whose length you want to determine; the len() function returns the total number of characters in that text, including spaces and punctuation marks.
12345string = "Practice is the key to success" length = len(string) print("Here is the string:", string) print("And its length is", length)
The len() function counts every character in the string, including spaces. In the example, "Practice is the key to success" has 30 characters, so len(text) returns 30.
Swipe to start coding
Time to practice:
- Get the last character of the variable
stringusing negative indexation and assign the result to the variablesymbol1. - Extract the index of the last character of the variable
stringusing thelen()method and assign the result to the variablesymbol2.
Note that len(word) equals the length of the word, but len(word)-1 equals the index of the last character of the word.
Solución
¡Gracias por tus comentarios!
single