“Operaciones de cadena de Python” Código de respuesta

cuerdas de pitón

# concatenating strings just means combining strings together
# it is used to add one string to the end of another
# below are two exmaples of how concatenation can be used 
# to output 'Hello World':

# example 1:
hello_world = "Hello" + " World"
print(hello_world)

>>> Hello World

# example 2:
hello = "Hello"
print(hello + " World")

>>> Hello World
codeconnoisseur

Funciones básicas de cadena en Python

# Basic Functions
len('turtle') # 6

# Basic Methods
'  I am alone '.strip()               # 'I am alone' --> Strips all whitespace characters from both ends.
'On an island'.strip('d')             # 'On an islan' --> # Strips all passed characters from both ends.
'but life is good!'.split()           # ['but', 'life', 'is', 'good!']
'Help me'.replace('me', 'you')        # 'Help you' --> Replaces first with second param
'Need to make fire'.startswith('Need')# True
'and cook rice'.endswith('rice')      # True
'bye bye'.index('e')                  # 2
'still there?'.upper()                # STILL THERE?
'HELLO?!'.lower()                     # hello?!
'ok, I am done.'.capitalize()         # 'Ok, I am done.'
'oh hi there'.find('i')               # 4 --> returns the starting index position of the first occurrence
'oh hi there'.count('e')              # 2
Tejas Naik

cuerdas de pitón

type('Hellloooooo') # str

'I\'m thirsty'
"I'm thirsty"
"\n" # new line
"\t" # adds a tab

'Hey you!'[4] # y
name = 'Andrei Neagoie'
name[4]     # e
name[:]     # Andrei Neagoie
name[1:]    # ndrei Neagoie
name[:1]    # A
name[-1]    # e
name[::1]   # Andrei Neagoie
name[::-1]  # eiogaeN ierdnA
name[0:10:2]# Ade e
# : is called slicing and has the format [ start : end : step ]

'Hi there ' + 'Timmy' # 'Hi there Timmy' --> This is called string concatenation
'*'*10 # **********
Tejas Naik

Operaciones de cadena de Python

# Python String Operations
str1 = 'Hello'
str2 ='World!'

# using +
print('str1 + str2 = ', str1 + str2)

# using *
print('str1 * 3 =', str1 * 3)
SAMER SAEID

cuerda de pitón

#Strings in python are surrounded by either single quotation marks, or double quotation marks.
print("This is a string")
print('i am also a string')
Programmer of empires

cuerda de pitón

string = 'amaama'
half = int(len(string) / 2)
 
if len(string) % 2 == 0:  # even
    first_str = string[:half]
    second_str = string[half:]
else:  # odd
    first_str = string[:half]
    second_str = string[half+1:]
 
# symmetric
if first_str == second_str:
    print(string, 'string is symmertical')
else:
    print(string, 'string is not symmertical')
 
# palindrome
if first_str == second_str[::-1]:  # ''.join(reversed(second_str)) [slower]
    print(string, 'string is palindrome')
else:
    print(string, 'string is not palindrome')

Respuestas similares a “Operaciones de cadena de Python”

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código