How to set a character after x lenght in a string [duplicate] - python

This question already has answers here:
Split the string into different lengths chunks
(6 answers)
Closed 4 years ago.
I'm trying to create a script that automatically replaces a minecraft uuid from b75243c9b8534269b885b036875a627c (without dashes) to b75243c9-b853-4269-b885-b036875a627c (with dashes) in python. But I have no idea how. Can you help me?
Thanks!

A quick and easy, albeit not as performative, way is to use uuid.UUID class and pass in your string as an argument
import uuid
my_uuid = uuid.UUID('b75243c9b8534269b885b036875a627c')
print(str(my_uuid))
>>> b75243c9-b853-4269-b885-b036875a627c

uuid = b75243c9b8534269b885b036875a627c
uuid = uuid[0:8] + "-" + uuid[8:12] + "-" + uuid[12:16] + "-" +
uuid[16:20] + "-" + uuid[20:]
As strings in python are immutable, you cannot insert a character in between. You can slice the given string and add ' - '.

Related

Python: Difference between "" and " " when creating a new variable with a empty string?? ( new_variable = " " ) [duplicate]

This question already has answers here:
Why is True returned when checking if an empty string is in another?
(5 answers)
Why empty string is on every string? [duplicate]
(2 answers)
Why does every string contain the empty string?
(2 answers)
Closed last month.
Oke, i've looked into How to check if the string is empty? but it didn't help me. Also chat GPT talks weird if you dig into this question, and the python manual didnt help me either.
Language = python 3.11.1
previous_char = " "
vowels = 'aeiou'
print(previous_char in vowels)
this code evaluates as 'false' and length 1
But if you remove the space between the quotation marks in previous_char
previous_char = ""
vowels = 'aeiou'
print(previous_char in vowels)
this code evaluates as 'true' and length 0
So basically if you ask: is 'nothing' in vowels.. its true??
I don't find this logical, but on the other hand, if it would evaluate to false, it would also be weird.
I started coding 2 weeks ago for fun, i'm 35 years old, so please don't burn me to hard if this is some kind of dumb question.
But i'm a bit stuck in understanding why this is the way it is?
" " is a string containing a single character (a space).
"" is the empty string.

Are python strings can be mutable? [duplicate]

This question already has answers here:
Aren't Python strings immutable? Then why does a + " " + b work?
(22 answers)
How to add a string in a certain position?
(10 answers)
Closed 1 year ago.
As string is immutable,so we can't change the string so how we can insert a character at middle position?
code:
s = "hello world"
s[5] = '-'
But it gives you error as it is immutable.so,how we can resolve this problem?
We know string is immutable,but we can't change values through assignment operator.so we can acheive this through string slicing:
s = s[:5]+'-'+s[6:]
so now s becomes "hello-world".
so this can be done using string slicing.
Yes , the strings in the Python are immutable.
But we can perform concatenate operation on strings.
If we want to modify string like..
S = "Hello World"
S[5] = '-'
It is not possible but we can do this by slicing method
S = S[:5] + '-' + S[6:]
Then the result is
S = "Hello-World"

The string becomes left-to-right When I write a number at the beginning of a string [duplicate]

This question already has answers here:
Why words are shuffled when I insert English words in any Arabic/Urdu/Persian text on Notepad or MS Word?
(8 answers)
How to make the text direction from right to left
(12 answers)
Closed 1 year ago.
I have a number string and a Persian string that I want to concatenate in python (my IDE is Pycharm) and when I do this, right-to-left breaks down.
num = "1200"
body = "ریال"
total = num + " " + body
print(total)
it results:
1200 ریال
but I expect this:
‏1200 ریال
what can I do?
There is a special standard character named Right-to-left mark.
you can use it with this expression:
u"\u200F"
So you can correct your code this way:
corrected = u"\u200F" + num + " " + body
print(corrected)
that results:
‏1200 ریال
the string isn't right to left, you're just adding the number first.
try:
total = body + " " + num

How to delete certain words from string without spaces? [duplicate]

This question already has answers here:
How to remove substring from string in Python 3
(2 answers)
Closed 2 years ago.
Is there I way to delete words from a string in Python if it doesn't have spaces. For example, if you have the string "WUBHELLOWUB" I want to remove "WUB". I tried
s = 'WUBHELLOWUB'
while 'WUB' in s:
ind = s.find('WUB')
s = s[:ind] + s[ind+1:]
print(s)
but it did not work.
You can use regex
import re
data=r"\S*WUB\S*"
re.sub(data, '','WUBWUBHELLO')

The ".replace" operator isn't working Python [duplicate]

This question already has answers here:
Why doesn't calling a string method (such as .replace or .strip) modify (mutate) the string?
(3 answers)
Why isn't the replace() function working? [duplicate]
(1 answer)
Closed 2 years ago.
I was testing some mechanics out and ran into an issue, the following program should replace the '+' sight to ' + '. The output of this theoretically should be '20 + 20', but in reality, it's '20+20'. I have no idea why.
string = "20+20"
if string.find(" ") == -1:
string.replace("+", " + ")
print(string)
In order for this to work, you need to reassign the string variable with the result of string.replace as the replace function returns the new string.
string = "20+20"
if string.find(" ") == -1:
string = string.replace("+", " + ")
print(string)

Categories