Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the jetpack domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u540705931/domains/danielbenites.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the rank-math-pro domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u540705931/domains/danielbenites.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the wpforms-lite domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u540705931/domains/danielbenites.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the breadcrumb-navxt domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u540705931/domains/danielbenites.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the advanced-ads domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u540705931/domains/danielbenites.com/public_html/wp-includes/functions.php on line 6114

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the post-views-counter domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see Debugging in WordPress for more information. (This message was added in version 6.7.0.) in /home/u540705931/domains/danielbenites.com/public_html/wp-includes/functions.php on line 6114
Como extraer numeros de un String en Python | Daniel Benites

Como extraer numeros de un String en Python - Sin morir en el Intento

En este articulo aprenderemos a extraer números de una cadena de caracteres(String) de una manera fácil y rápida. 

Anuncios

Que es un String en Python?

Un String o cadena es una matriz de bytes que representan caracteres Unicode entre comillas simples, dobles o triples. Los caracteres encerrados pueden ser números, letras o caracteres especiales. Un String o cadena es solo un texto normal y es legible por humanos. Las cadenas son inmutables en Python. significa que una vez que se define un objeto de cadena, no se puede cambiar.

				
					cadena = input("Esto que escribiras es una cadena: ")
				
			

De modo tal que si ingresamos el código anterior en nuestro editor y lo ejecutamos, nos pedirá que ingresemos algo desde nuestro teclado.

Podemos fácilmente comprobar que lo es haciendo uso del comando type de la siguiente forma:

				
					print(type(cadena))
				
			

Lo que me devolverá que tipo de datos posee la variable cadena

En este caso muy sencillo, vamos a entrar por teclado una cifra de dos números, vamos a extraer los números y luego los sumaremos.

Ya con los numeros introducidos lo que nos queda es crear una nueva variable si asi se decide para que asuma el valor del primer character introducido anteriormente, aunque no es necesario, ya que podemos utilizar el valor directamente.

Y lo que vamos hacer para que este valor se vuelva numero es anteceder el «int» a la extraccion de caracteres:

				
					int(cadena[0])
				
			

Esto me va a devolver el primer caracter introducido en la cadena anterior, ya que como recordaran el index empieza desde 0, si por el contrario quisiera sacar el segundo valor deberia poner:

				
					int(cadena[1])
				
			

Los valores ya podrian ser utilizados en cualquier operacion aritmetica, por ejemplo una suma:

				
					suma = int(cadena[0])+int(cadena[1])
print(suma)
				
			

Y esto nos devolvería la suma del primero con el segundo.

Es hora de que lo pruebes tu y me cuentes como te fue.

Nos vemos en la próxima. 

Anuncios